使用LINQ在c#中解析XML

4
在这个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,对应于字段名为"name"的字段,其中字段名为"age"的值是Vincent。我该如何获得这个结果?
2个回答

7

我建议您像阅读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);

它是否满足你的需求?


document.Root.Element("resources")... 在使用时我需要添加什么?使用 system.xml 和 system.xml.linq,文档无效。对于 CultureInfo 也是一样。 - Vincenzo Lo Palo
我已经编辑了_document.Root.Element_的帖子。 对于_CultureInfo_,它在_System.Globalization_中。 - Cédric Bignon
我有以下输出: Vincent: 31 John: 27 Michael: 20 Frank: 25我只需要31。 - Vincenzo Lo Palo
现在我有31 27 20 25。为了我的目的,我只需要特定的值31才能输出。 - Vincenzo Lo Palo
是的,我正在更新代码 ;) 现在只有“Vincent”的结果。如果有多个“Vincent”,你想做什么? - Cédric Bignon

0

考虑一下,下面的XML作为SQL表列之一存在。

<Root>
  <Name>Dinesh</Name>
  <Id>2</Id>
</Root>

查询的目标是从XML中获取名称。在这个例子中,我们将获取“Dinesh”作为值。
var Query = (from t in dbContext.Employee.AsEnumerable()
where t.active == true 
select new Employee
{
    Id = t.AtpEventId,  
    Name = XDocument.Parse(t.Content).Descendants("Root").Descendants("Name").ToList().  
    Select(node => node.Value.ToString()).FirstOrDefault()  
});

请注意以下内容:
  1. t.active == true 只是一个示例,如果需要的话可以使用一些条件。

  2. 请注意,在上面的LINQ查询中,始终使用AsEnumerable,就像我在第一行中所做的那样。

  3. Descendants("Root").Descendants("Name") - 这里的 "Root" 应该是与 XML 匹配的元素,在 Root 下面我们有 Name 元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接