C#中的LINQ to XML查询 [英] C# Linq to XML query
本文介绍了C#中的LINQ to XML查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<世界与GT;
<动物>
<标签>
将;狗ID =1>
< DOG1>< / DOG1>
<&DOG2 GT;< / DOG2>
< Dog3>< / Dog3>
< /狗>
<狗ID =2>< /狗>
<狗ID =3>< /狗>
< /标签>
< /动物和GT;
< /世界>
我如何得到标记下的所有元素其中id == 1?
我的Linq查询。 ?(不工作),为什么
的XDocument XML = XDocument.Load(xml.xml);
变种元素从E =在xml.Descendants(动物)。后代(标签)。元素(狗)
其中e.Attribute(ID)。的toString()。等于(1)
选择C;
你能检查一下好吗?
谢谢!
解决方案
VAR的结果= xdoc.Descendants(世界)
.Descendants(动物)
.Descendants(标签)
.Elements(狗)
。其中(n =方式> n.Attribute(ID)值==1);
输出:
<预类=朗XML prettyprint-覆盖>
<狗ID =1>
< DOG1>< / DOG1>
<&DOG2 GT;< / DOG2>
< Dog3>< / Dog3>
< /狗>
<World>
<Animals>
<Tab>
<Dogs id ="1">
<Dog1></Dog1>
<Dog2></Dog2>
<Dog3></Dog3>
</Dogs>
<Dogs id ="2"></Dogs>
<Dogs id ="3"></Dogs>
</Tab>
</Animals>
</World>
How do I get all elements under tag where id == 1?
My Linq query. (doesn't work) why?
XDocument xml= XDocument.Load(xml.xml);
var elements = from e in xml.Descendants("Animals").Descendants("Tab").Elements("Dogs")
where e.Attribute("id").toString().Equals("1")
select c;
Could you check it please?
Thanks!
解决方案
var result = xdoc.Descendants("World")
.Descendants("Animals")
.Descendants("Tab")
.Elements("Dogs")
.Where(n => n.Attribute("id").Value == "1");
Output:
<Dogs id="1">
<Dog1></Dog1>
<Dog2></Dog2>
<Dog3></Dog3>
</Dogs>
这篇关于C#中的LINQ to XML查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文