Reputation: 10156
I have XElement
object which is my XML tree read from XML file. Now I want to check all the nodes in this tree to get first attribute name and value. Is there any simple way to go through all of the nodes (from root till leaves)? My XML file has got very many different and strange nodes - that's why it's harder to solve this issue. I thought about writing some recursion, but hope it's another way to solve that easier.
Upvotes: 1
Views: 2791
Reputation: 16621
You can get all children elements using XElement.Elements()
.
Here's some code using recursion to get all elements of each level:
void GetElements(XElement element){
var elements = element.Elements();
foreach(Element e in elements){
//some stuff here
if(e.Elements() != null)
GetElements(e);
}
}
Upvotes: 1
Reputation: 3727
Maybe take a look to Xpath. an XPath like this //*[@id=42]
could do the job.
It means get all nodes which have an attribute "id" of value 42.
You can do just //*
which gonna returns all nodes in a tree.
Xpath : http://msdn.microsoft.com/en-gb/library/ms950786.aspx
Syntax : http://msdn.microsoft.com/en-us/library/ms256471.aspx
Upvotes: 2