获取项目的深度(Get the depth of an Item)

编程入门 行业动态 更新时间:2024-10-09 14:21:34
获取项目的深度(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.

更多推荐

本文发布于:2023-08-06 12:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1449352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:深度   项目   Item   depth

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!