Csharp/C#教程:C#Linq到XML查询分享


C#Linq到XML查询

             

如何在tag == 1的标签下获取所有元素?

我的Linq查询。 (不起作用)为什么?

 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; 

你能检查一下吗?

谢谢!

 var result = xdoc.Descendants("World") .Descendants("Animals") .Descendants("Tab") .Elements("Dogs") .Where(n => n.Attribute("id").Value == "1"); 

输出:

      

或者使用XPath:

 xml.XPathSelectElements("/World/Animals/Tab/Dogs[@id=1]") 

要么

xml.XPathSelectElements("//Dogs[@id=1]")

无论发生在哪里都可以找到所有狗。

根据您认为您想要的样本数据

 //from e in xml.Descendants("Animals").Descendants("Tab").Elements("Dogs") from e in xml.Descendants("Animals").Elements("Tab").Descendants("Dogs") 

在同一行中,如果你想强制执行结构, Descendants("Animals")可能是Elements("Animals")

其余的查询看起来没问题。

上述就是C#学习教程:C#Linq到XML查询分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1000461.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐