通过属性名称选择元素 [英] Select elements by attribute name

查看:64
本文介绍了通过属性名称选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来与此类似的XML文件:

Hi I have an XML file that looks similar to this:

<doc>
  <item type="1">HelloWorld</item>
  <item type="2">LINQ</item>
  <item type="3">C#</item>
</doc>



我有一个带有3个字符串变量a,b和amp;的Item类. c,目前这就是我正在做的事情:



I have an Item class with 3 string variables a,b & c and currently this is what I''m doing:

var Results = from docs in result.Descendants("doc")
              select new Item
              {
                 a = (from docs2 in docs.Descendants("item") 
                      where docs2.Attribute("type").Value == "1"
                      select docs2.Value).First(),
                 b = (from docs2 in docs.Descendants("item")
                      where docs2.Attribute("type").Value == "2"                      
                      select docs2.Value).First(),
                 c = (from docs2 in docs.Descendants("item")
                      where docs2.Attribute("type").Value == "3"
                      select docs2.Value).First()
              };



我无法更改XML文件的格式.
有更好的方法吗?



I''m unable to change the format of the XML file.
Is there a better way of doing this?

推荐答案

尝试以下链接:

LINQ to XML [处理XML数据
XPath和XmlDocument(C#)
[ ^ ]
Try the below link:

LINQ to XML[^]

Manipulate XML data
with XPath and XmlDocument (C#)
[^]


这篇关于通过属性名称选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆