Using XLinq, you seem to have to know the exact name of an element's children to access specific child elements. I want to access/discover the immediate children of an element recursivly without such tight coupling.
if I have:
<root>
<level1/>
<level1/>
<level1>
<level2.1>
<level2.2>
<level2.2.1/>
</level2.2>
</level1>
<level1/>
</root>
In looking for "root"'s children, root.Elements().Count() returns 7 - I want 4 (the "level1" nodes). If I ask root.Elements("level1"), I get 4. But I have to know the name of the child elements.
Question: How can I access the immediate children without knowing that element name? And this would be at any recursive spot in the XML tree?
thanks.
---newly add: code --q1 == 7, q2 == 8 and q3 == 4. BUT, if you iterate through the elements of q3, you access all 7 child nodes rather than the 4 I'd want. If this means having to just to an ElementAt(#), fine. But the object seems to have a conflict between what it "sees" as it's children.
XElement xel = new XElement(
new XElement("root",
new XElement("level1"),
new XElement("level1"),
new XElement("level1",
new XElement("level2.1"),
new XElement("level2.2",
new XElement("level2.2.1"))),
new XElement("level1")
));
var q1 = from x in xel.Descendants()
select x;
var q2 = from x in xel.DescendantsAndSelf()
select x;
var q3 = from x in xel.Elements()
select x;
foreach (XElement x in q3.Elements())
{
string s = x.ToString();
}