Linq在xml中选择子节点 [英] Linq in xml selecting child nodes
本文介绍了Linq在xml中选择子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个xml文件
Hi , i hav a xml file
<parent>
<child>
<tag>AAA</tag>
<att>BBB</att>
</child>
<parent>
如何获得< tag>的值&安培; < ATT>使用Linq
我是xml和linq的新手
plz help!
how to get the value of <tag> & <att> using Linq
I am new to xml and linq
plz help!
推荐答案
您需要至少引用这两个名称空间:
You need to reference at least these two namespaces:
using System.Linq;
using System.Xml.Linq;
并尝试以下代码:
And try this code:
var xml = @"
<parent>
<child>
<tag>AAA</tag>
<att>BBB</att>
</child>
<child>
<tag>CCC</tag>
<att>DDD</att>
</child>
</parent>";
var xDoc = XDocument.Parse(xml);
var query = from i in xDoc.Root.Descendants("child")
select new
{
Tag = i.Descendants("tag").First().Value,
Att = i.Descendants("att").First().Value
};
var items = query.ToArray();
请记住,你可以使用任何LINQ扩展方法,如Select,Single,Count,Sum等。
Remember, that you can use any LINQ extension methods, like Select, Single, Count, Sum, etc.
这篇关于Linq在xml中选择子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文