gpt4 book ai didi

c# - 使用 Linq to XML 时查询 xml 返回 null

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:02 25 4
gpt4 key购买 nike

我在文件中有以下 xml:

<Person>
<Name first="John" last="Doe" />
</Person>

我用 XDocument.Load 加载了 xml 文档,但我似乎无法获取第一个和最后一个属性的值。

我试过:

var q = from n in rq.Element("Name")
select n; //but q is null after this.

最佳答案

下面是一个适用于您的 XML 文件的示例:

var doc = XDocument.Load(...);

var query = from node in doc.Root.Elements("Name")
select new // ↑
{
First = (string)node.Attribute("first"),
Last = (string)node.Attribute("last")
};

foreach (var item in query)
{
Console.WriteLine("{1}, {0}", item.First, item.Last);
}

关于c# - 使用 Linq to XML 时查询 xml 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5985506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com