Little help with XLinq
I have this XML:
<Test>
<element>toto</element>
<element>tata</element>
</Test>
How开发者_运维技巧 I can get the nodes "element"?. I see on the web I can get them with:
var elements = from element in xmlDoc.Descendants("element")
select element;
But "elements" is empty!
EDIT 1: I'm loading the XDocument with this exact XML:
<Test xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
<element>toto</element>
<element>tata</element>
</Test>
Ok well there's your problem, your names have to be qualified with the appropriate XML namespace.
XNamespace ns = "http://schemas.microsoft.com/ado/2007/08/dataservices";
var elements = xmlDoc.Descendants(ns + "element");
It is probably a problem with how you are creating your XDocument
object. The following code works fine for me:
var doc = XDocument.Parse(@"
<Test>
<element>toto</element>
<element>tata</element>
</Test>");
var elements = doc.Descendants("element");
//prints "2"
Console.WriteLine(elements.Count());
精彩评论