【问题标题】:Parsing XML using LINQ in c#在 C# 中使用 LINQ 解析 XML
【发布时间】:2013-01-06 08:19:58
【问题描述】:

在这个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,它对应于字段名称 =“年龄”,其中字段名称 =“名称”是文森特。我怎样才能得到这个结果?

【问题讨论】:

    标签: c# xml-parsing linq-to-xml


    【解决方案1】:

    考虑下面的 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 元素。

    【讨论】:

      【解决方案2】:

      我建议您像自然阅读 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 的文档无效。文化信息也一样
      • 我已经编辑了 document.Root.Element 的帖子。对于CultureInfo,它位于System.Globalization
      • 我有这个输出:文森特:31 约翰:27 迈克尔:20 弗兰克:25 我只需要 31
      • 现在我有 31 27 20 25。就我的目的而言,我只需要特定值 31 即可获得输出
      • 是的,我正在更新代码;)现在它只是“Vincent”的结果。如果有多个“文森特”,你想做什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多