ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

在c#中使用LINQ解析XML

2019-06-13 04:03:12  阅读:183  来源: 互联网

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


在这个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并检查所有资源元素更自然.然后在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,xml-parsing,linq-to-xml
来源: https://codeday.me/bug/20190613/1230410.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有