获取该产品的深度 [英] Get the depth of an Item

查看:82
本文介绍了获取该产品的深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XML:

<$p$p><$c$c><A><B>test</B><B><B>test2</B></B><B><B><B>test2</B></B></B></A>

我怎样才能得到每一个使用LINQ to XML这些项目的水平

测试= 1级测试2 = 2级TEST3的水平= 3

我不知道有多少节点有怎么会或多少级会有的。我可以写这是一个递归函数,但我认为的LINQ to XML可能有一些更好的报价。


解决方案

假设您已经加载了XML作为的XDocument 的XElement 对象,

myXElement.AncestorsAndSelf()。COUNT()

应该给你任何特定元素的深度。

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.

这篇关于获取该产品的深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆