簡體   English   中英

計算XDocument上的子節點

[英]Count child nodes on XDocument

有沒有辦法計算XDocument上的子節點?

我找了一個計數方法或屬性,找不到一個。

謝謝獅子座

var doc = XDocument.Load(fileName);
int descendantsCount = doc.Descendants().Count(); // counts ALL descendants elements
int childrenCount = doc.Root.Elements().Count(); // counts direct children of the root element

或者......如果您知道元素的名稱永遠不會改變並且它們始終存在,

XDocument xD = XDocument.Load(XmlFullFileName);
XElement xE_ParameterSets = xD.Root.Element("Report").Element("ParameterSets");
int index = ((IEnumerable<XElement>)xE_ParameterSets.Elements()).Count();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM