Linq: read node innertext
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.
精彩评论