I have xml like this:
<A><B>test</B><B><B>test2</B></B><B><B><B>test2</B></B></B></A>
How can I get the level of each of these items using linq to xml
level of test=1 level of test2=2 level of test3=3
I have no idea how many nodes there will be or how many levels there will be. I can write this as a recursive function but I thought linq to xml might have something better to offer.
Assuming you've loaded your XML as an XDocument
or XElement
object,
myXElement.AncestorsAndSelf().Count()
should give you the depth of any given element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With