C# 中的 Linq 到 xml

发布于 2024-12-18 17:52:27 字数 655 浏览 1 评论 0原文

场景:

  1. 网格视图填充到 WPF 窗口中。
  2. 在代码后面有一个静态列表。(我想从 xml 文件中获取)。

尝试将静态列表移动到 xml 文件中。为此,我以以下格式创建了一个 ml 文件

<customers>
<customer Name="abc"/>
<customer Name="def"/>
</customers>

CodeBehind:

Xdocument doc=Xdocument.load("customers.xml");
var customerList = (from e in doc.Descendants("Cusomters")

                            select new
                            {
                                CustomerName = e.Attribute("Name").Value
                            }).ToList();

我无法将客户名称从 xml 文件移动到 customerList。如果有人可以帮助我移动,我将不胜感激向前。

Scenario:

  1. Grid view gets populated in WPF window.
  2. Having a static list in code behind.(which i want to get from a xml file).

Trying to move the static list into an xml file.For that i created a ml file in the following format

<customers>
<customer Name="abc"/>
<customer Name="def"/>
</customers>

CodeBehind:

Xdocument doc=Xdocument.load("customers.xml");
var customerList = (from e in doc.Descendants("Cusomters")

                            select new
                            {
                                CustomerName = e.Attribute("Name").Value
                            }).ToList();

I am unable to get the customer names from the xml file to the customerList.I would appreciate if someone can help me to move forward.

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

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

发布评论

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

评论(4

感情洁癖 2024-12-25 17:52:27

“Customers” 拼写错误,应为 “Customers”

显然这不是您正在使用的代码,因为它甚至无法编译。应该是这样的:

XDocument doc = XDocument.Load( "customers.xml" );
var customerList = (from e in doc.Descendants( "customer" )
        select new
        {
            CustomerName = e.Attribute( "Name" ).Value
        }).ToList();

你真的应该提到它无法编译的事实。那,或者您错误地手工复制了它,这也无助于我们为您提供帮助。

这里的逻辑问题是您要求所有 Customers 标签,请注意末尾的 s 。您确实想要查找具有 name 属性的 Customer 标记。 Customer*s* 只是顶级组。

"Cusomters" is spelled incorrectly, should be "Customers".

Obviously this is not the code your are using since it doesn't even compile. It should be this:

XDocument doc = XDocument.Load( "customers.xml" );
var customerList = (from e in doc.Descendants( "customer" )
        select new
        {
            CustomerName = e.Attribute( "Name" ).Value
        }).ToList();

You really should mention the fact that it won't compile. That, or you copied it in by hand incorrectly, which doesn't help us help you either.

The logical problem here is that you are asking for all Customers tags, note the s at the end. You really want to look for Customer tags, which have a name attribute. Customer*s* is simply the top level group.

诗酒趁年少 2024-12-25 17:52:27

使用 customer 而不是 Cusomters (XML 区分大小写):

from e in doc.Descendants("customer")

Use customer instead of Cusomters (XML is case-sensitive):

from e in doc.Descendants("customer")
紫瑟鸿黎 2024-12-25 17:52:27

您很可能需要一个List,这样您就不需要投影到匿名类 - 您的查询中也存在拼写错误(“Cusomters”):

var customerList = (from e in doc.Descendants("Customer")
                    select e.Attribute("Name").Value).ToList();

或使用扩展方法语法:

var customerList = doc.Descendants("Customer")
                      .Select( e => e.Attribute("Name").Value)
                      .ToList();

You most likely want a List<string> so you don't need to project to an anonymous class - also there is a typo in your query ("Cusomters"):

var customerList = (from e in doc.Descendants("Customer")
                    select e.Attribute("Name").Value).ToList();

or with extension method syntax:

var customerList = doc.Descendants("Customer")
                      .Select( e => e.Attribute("Name").Value)
                      .ToList();
岁月苍老的讽刺 2024-12-25 17:52:27

我总是使用 :

doc.root.elements("Customer")

来表示这样的小片段。

Ive always used :

doc.root.elements("Customer")

for small snippets like this.

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