开发者

Linq: read node innertext

开发者 https://www.devze.com 2023-01-01 17:48 出处:网络
i have a xml document like this: <ns:a xmlns:ns=\"h开发者_JAVA技巧ttp://NS1\"> <ns:b> <c xmlns=\"http://differentNS\"> c_text </c>

i have a xml document like this:

<ns:a xmlns:ns="h开发者_JAVA技巧ttp://NS1">
  <ns:b>
    <c xmlns="http://differentNS"> c_text </c>
    <x xmlns="http://differentNS"> Wanted </x>
    <d xmlns="http://differentNS"> d_text </d>
  </ns:b>
</ns:a>

Now i want to use linq to read the element's "x" inner text.


Here's a possible implementation using LINQ to XML:

var doc = XDocument.Parse("<ns:a xmlns:ns='http://NS1'><ns:b><c xmlns='http://differentNS'>c_text</c><x xmlns='http://differentNS'>Wanted</x><d xmlns='http://differentNS'>d_text</d></ns:b></ns:a>");

XNamespace ns = "http://differentNS";
var result = doc.Descendants(ns + "x").Single().Value

Related resources:

  • XDocument.Parse method
  • XContainer.Descendants method


You should be able to do something like this:

var xDocument = XDocument.Load(yourdocumenthere);
var myvalue = xDocument.Element("ns:a").element("ns:b").element("c").value;

This isn't using link, but is still very simple.

0

精彩评论

暂无评论...
验证码 换一张
取 消