如何计算 XML 中已读入字符串的节点数
我确信这个解决方案对于有经验的人来说是相当微不足道的,但对我来说却不是。我已将 XML 文件读入字符串 strSiteList。以下是 XML 的简短示例:
<siteList>
<site code="s0000001">
<nameEn>Edmonton</nameEn>
<provinceCode>AB</provinceCode>
</site>
<site code="s0000002">
<nameEn>Vancouver</nameEn>
<provinceCode>BC</provinceCode>
</site>
</siteList>
How do I count the number of times a site comes? 我已经从这个开始了:
XDocument loaded = XDocument.Parse(strSiteList);
int sitesCount = loaded.Nodes().Count(d => "some code that should work...arg...";
但我不知道这是否是开始这个的正确方法。
谢谢!
I'm sure the solution is pretty trivial to the experienced, but it's not to me. I have read an XML file into a string, strSiteList. Here's a shortened sample of the XML:
<siteList>
<site code="s0000001">
<nameEn>Edmonton</nameEn>
<provinceCode>AB</provinceCode>
</site>
<site code="s0000002">
<nameEn>Vancouver</nameEn>
<provinceCode>BC</provinceCode>
</site>
</siteList>
How do I count the number of times a site appears?
I've started with this:
XDocument loaded = XDocument.Parse(strSiteList);
int sitesCount = loaded.Nodes().Count(d => "some code that should work...arg...";
But I am lost as to whether it's the right way to start this or not.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
特定网站?在我看来,这听起来像:
对于所有网站,只需:
A particular site? That sounds to me like:
For all sites, just:
您可以使用简单的 XPath 表达式(需要使用“System.Xml.XPath”):
You can use simple XPath expression (using "System.Xml.XPath" required):
假设它已被正确读取,您应该能够执行以下操作:
您还可以执行以下操作:
如果您正在查找所有站点的计数,您可以执行以下操作:
Assuming that it's been read in correctly, you should be able to do this:
You can also do this:
If you're looking for a count of all the sites, you can just do this: