gpt4 book ai didi

c# - 在 C# 中使用 LINQ 解析 XML

转载 作者:太空狗 更新时间:2023-10-30 00:42:37 27 4
gpt4 key购买 nike

在此 xml 文件 (http://www.studiovincent.net/list.xml) 中:

<list version="1.0">
<meta>
<type>resource-list</type>
</meta>
<resources start="0" count="4">
<resource classname="Quote">
<field name="name">Vincent</field>
<field name="username">Hill</field>
<field name="age">31</field>
<field name="hair">black</field>
</resource>
<resource classname="Quote">
<field name="name">John</field>
<field name="username">Tedelon</field>
<field name="age">27</field>
<field name="hair">brown</field>
</resource>
<resource classname="Quote">
<field name="name">Michael</field>
<field name="username">Lopez</field>
<field name="age">20</field>
<field name="hair">red</field>
</resource>
<resource classname="Quote">
<field name="name">Frank</field>
<field name="username">Lopez</field>
<field name="age">25</field>
<field name="hair">black</field>
</resource>
</resources>
</list>

使用此代码:

using System.Xml;
using.System.Xml.Linq;

XmlReader reader = XmlReader.Create("http://www.studiovincent.net/list.xml");
XElement el = XElement.Load(reader);
reader.Close();

var items = el.Elements("resources").Elements("resource").Descendants().DescendantNodes();

var items = from item in el.Elements("resources").Elements("resource").Descendants()
where item.Attribute("name").Value == "name" select item.FirstNode;

foreach (XNode node in items)
{
Console.WriteLine(node.ToString());
}

我有这个输出:

Vincent
John
Michael
Frank

代码运行良好,但我需要获取值 31,它对应于字段名称 =“年龄”,其中字段名称 =“名称”是 Vincent。我怎样才能得到这个结果?

最佳答案

我建议您像自然阅读 XML 时那样做。

在您的代码中,尝试找到所有 name 属性设置为 "name" 的字段。

此过程不能用于将姓名与年龄相关联。阅读 XML 并检查所有 resource 元素更为自然。然后向此元素添加一些在 field 元素中描述的信息。

// Using LINQ to SQL
XDocument document = XDocument.Load("http://www.studiovincent.net/list.xml"); // Loads the XML document.
XElement resourcesElement = document.Root.Element("resources"); // Gets the "resources" element that is in the root "list" of the document.

XElement resourceElementVincent = (from resourceElement in resourcesElement.Elements("resource")// Gets all the "resource" elements in the "resources" element
let fieldNameElement = resourceElement.Elements("field").Single(fieldElement => fieldElement.Attribute("name").Value == "name") // Gets the field that contains the name (there one and only one "name" field in the "resource" element -> use of Single())
where fieldNameElement.Value == "Vincent" // To get only resources called "Vincent"
select resourceElement).Single(); // We suppose there is one and only 1 resource called "Vincent" -> Use of Single()
XElement fieldAgeElement = resourceElementVincent.Elements("field").Single(fieldElement => fieldElement.Attribute("name").Value == "age"); // Gets the corresponding "age" field
int age = int.Parse(fieldAgeElement.Value, CultureInfo.InvariantCulture); // Gets the age by Parse it as an integer
Console.WriteLine(age);

它做你想做的事吗?

关于c# - 在 C# 中使用 LINQ 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14469802/

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