如何计算 XML 中已读入字符串的节点数

发布于 2024-12-07 00:27:47 字数 692 浏览 1 评论 0原文

我确信这个解决方案对于有经验的人来说是相当微不足道的,但对我来说却不是。我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

停顿的约定 2024-12-14 00:27:47

特定网站?在我看来,这听起来像:

string site = "s0000002";
XDocument loaded = XDocument.Parse(xml);
int sitesCount = loaded.Descendants("site")
                       .Count(x => (string) x.Attribute("code") == site);

对于所有网站,只需:

int sitesCount = loaded.Descendants("site").Count();

A particular site? That sounds to me like:

string site = "s0000002";
XDocument loaded = XDocument.Parse(xml);
int sitesCount = loaded.Descendants("site")
                       .Count(x => (string) x.Attribute("code") == site);

For all sites, just:

int sitesCount = loaded.Descendants("site").Count();
墨离汐 2024-12-14 00:27:47

您可以使用简单的 XPath 表达式(需要使用“System.Xml.XPath”):

XDocument loaded = XDocument.Parse(xml);
int sitesCount = loaded.XPathSelectElements("siteList/site").Count();

You can use simple XPath expression (using "System.Xml.XPath" required):

XDocument loaded = XDocument.Parse(xml);
int sitesCount = loaded.XPathSelectElements("siteList/site").Count();
深府石板幽径 2024-12-14 00:27:47

假设它已被正确读取,您应该能够执行以下操作:

int sitesCount = loaded.Descendants("site").Where(x => x.Attribute("code").Value == "1234").Count();

您还可以执行以下操作:

int sitesCount = loaded.Descendants("site").Count(x => x.Attribute("code").Value == "1234");

如果您正在查找所有站点的计数,您可以执行以下操作:

int sitesCount = loaded.Descendants("site").Count();

Assuming that it's been read in correctly, you should be able to do this:

int sitesCount = loaded.Descendants("site").Where(x => x.Attribute("code").Value == "1234").Count();

You can also do this:

int sitesCount = loaded.Descendants("site").Count(x => x.Attribute("code").Value == "1234");

If you're looking for a count of all the sites, you can just do this:

int sitesCount = loaded.Descendants("site").Count();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文