获取项目的深度(Get the depth of an Item)
我有这样的xml:
<A><B>test</B><B><B>test2</B></B><B><B><B>test2</B></B></B></A>如何使用linq到xml获取每个项目的级别
测试水平= 1水平测试2 = 2水平测试3 = 3
我不知道将会有多少节点或将有多少级别。 我可以把它写成递归函数,但我认为linq to xml可能有更好的东西。
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.
最满意答案
假设您已将XML加载为XDocument或XElement对象,
myXElement.AncestorsAndSelf().Count()
应该给你任何给定元素的深度。
Assuming you've loaded your XML as an XDocument or XElement object,
myXElement.AncestorsAndSelf().Count()
should give you the depth of any given element.
更多推荐
发布评论