如何将 XDocument 元素放入组合框(第二个框依赖于第一个框)(C# .net 3.5)

发布于 2024-12-07 11:27:15 字数 877 浏览 1 评论 0原文

我正在尝试获取 XML 并显示两个组合框。第一个组合框将包含与provinceCode 元素不同的列表(即,没有重复项)。第二个组合框将仅显示与provinceCode匹配的nameEN元素。这是我的 XML 示例:

<siteList>
  <site code="s0000001">
    <nameEn>Edmonton</nameEn>
    <provinceCode>AB</provinceCode>
  </site>
  <site code="s0000002">
    <nameEn>Vancouver</nameEn>
    <provinceCode>BC</provinceCode>
  </site>
...
</siteList>

我的 XDocument 来源于此:

XDocument loaded = XDocument.Parse(strSiteList);

我正在努力解决如何提取唯一的省份列表的问题。它是这样的:

var list = loaded.Descendants("provinceCode").Distinct;

但我是 C# 和 XDocument 的新手,我不知道要使用什么类型的变量,所以我得到“无法将方法组分配给隐式类型的局部变量”。

我完全不知道如何处理组合框。我在 stackoverflow 和 google 上进行了快速搜索,但似乎没有任何与 C# 中的 XDocument 和依赖组合框相关的内容。使用 XDocument 是错误的方法吗?

谢谢!

I'm trying to take XML and display two comboboxes. The first combobox will contain a distinct list (i.e., no duplicates) from the provinceCode elements. The second combobox will show only nameEN elements matching the provinceCode. Here is a sample of my XML:

<siteList>
  <site code="s0000001">
    <nameEn>Edmonton</nameEn>
    <provinceCode>AB</provinceCode>
  </site>
  <site code="s0000002">
    <nameEn>Vancouver</nameEn>
    <provinceCode>BC</provinceCode>
  </site>
...
</siteList>

I have my XDocument from this:

XDocument loaded = XDocument.Parse(strSiteList);

I'm struggling with how to extract the unique list of provinces. It's something like:

var list = loaded.Descendants("provinceCode").Distinct;

but I'm new to C# and XDocument and I don't know what type of variable to use so I get "Cannot assign method group to an implicitly-typed local variable".

And I'm totally in the dark on how do to the comboboxes. I've done a quick search on stackoverflow and google, but there doesn't seem to be anything relevant on both XDocument and dependent comboboxes in C#. Is using XDocument the wrong approach?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

谈场末日恋爱 2024-12-14 11:27:15

问题是您实际上并没有调用该方法。你可以这样写:

var list = loaded.Descendants("provinceCode").Distinct();

注意括号。

但我认为这实际上并没有达到你想要的效果。我怀疑你想要:

List<string> list = loaded.Descendants("provinceCode")
                          .Select(x => x.Value)
                          .Distinct()
                          .ToList();

这会给你一个不同省份代码的列表作为字符串列表。

目前还不太清楚您对第二个列表的含义 - 这是否意味着仅匹配单个省份代码?如果是这样:

List<string> names = loaded.Descendants("site")
                .Where(x => x.Element("provinceCode").Value == provinceCode) 
                .Select(x => x.Element("nameEn").Value)
                .ToList();

The problem is that you're not actually calling the method. You could write:

var list = loaded.Descendants("provinceCode").Distinct();

Note the brackets.

but I don't think that actually does what you want. I suspect you want:

List<string> list = loaded.Descendants("provinceCode")
                          .Select(x => x.Value)
                          .Distinct()
                          .ToList();

That will give you a list of distinct province codes as a list of strings.

It's not quite clear what you mean about the second list - is that meant to match just a single province code? If so:

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