So I have a simple XML Doc like this:
<Xml>
<Node1></Node1>
<Node2></Node2>
...
<Node10></Node10>
<Node10Stuff1></Node10Stuff1>
<Node10Stuff2></Node10Stuff2>
<Node11></Node11>
</Xml>
Since I’m getting the XML Using a post, I’m assigning it my XDocument like so:
XDocument xd;
using(StringReader s = new StringReader(postXml))
{
xd = XDocument.Load(s);
}
And here is my LINQ query:
var q = from c in xd.Root.Elements("Xml")
select c;
foreach(var a in q)
{
String node1= a.Element("Node1").ToString();
...etc...
}
But my foreach loop is always empty. Shouldnt my query be returning all of the nodes? At least the children of <Xml>?
xd.Rootis already the<Xml>element – you’re currently looking for<Xml>elements below that, which is why you’re not finding anything.It looks like you don’t even need the
foreachloop, just: