2011-08-18 7 views
8

XDocument上の子ノードを数える方法はありますか?XDocument上の子ノードをカウントする

カウントメソッドまたはプロパティを検索したところ、見つからなかった。

おかげで レオ

また
+0

ことを私はxdocumentを使用して方法を見つけました。 Root.Nodes()。Count()、これが最善の方法であるかどうかわかりません。ありがとうございます – MammothOne

+0

明確にしてください。 XDocumentは、XMLデータを含むドキュメントです。ドキュメントの行の総数を知りたいですか?または、ドキュメントのルート要素の子ノードを知りたいですか? –

+0

ダニエル、お返事ありがとうございます。これは私が探していたものです。 doc.Descendants()。Count(); ありがとうThomas。 – MammothOne

答えて

14
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 
2

は...あなたが知っていれば要素の名前を変更するつもりはありません、彼らは常に存在し、

XDocument xD = XDocument.Load(XmlFullFileName); 
XElement xE_ParameterSets = xD.Root.Element("Report").Element("ParameterSets"); 
int index = ((IEnumerable<XElement>)xE_ParameterSets.Elements()).Count(); 
関連する問題