考虑以下XML:
<Employees>
<Person>
<ID>1000</ID>
<Name>Nima</Name>
<LName>Agha</LName>
</Person>
<Person>
<ID>1001</ID>
<Name>Ligha</Name>
<LName>Ligha</LName>
</Person>
<Person>
<ID>1002</ID>
<Name>Jigha</Name>
<LName>Jigha</LName>
</Person>
<Person>
<ID>1003</ID>
<Name>Aba</Name>
<LName>Aba</LName>
</Person>
</Employees>
我声明了一个XElement
变量并将XML分配给它。如何在C#中获取此XML变量中ID
元素的计数?
Elements("Employees")
,它会返回0 :-? - ArianEmployees
是根节点。如果你使用的是XElement
,那么查询中就不需要包含Elements("Employee")
。如果它不是根元素,那么你可以选择包含它,或者使用xml.Descendants("Person").Elements("ID").Count()
,从而避免逐个遍历 XML 元素的需要。 - Ahmad MageedDescendants()
和Elements()
都返回一个IEnumerable
,它似乎没有Count()
方法。我是否漏掉了一些明显的东西? - Jon Heggland