我如何查找属性使用LINQ to XML XML元素? [英] How do I find an XML element by attribute using LINQ to XML?
本文介绍了我如何查找属性使用LINQ to XML XML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我学习的LINQ to XML和需要找到一个元素的存在具有特定属性。目前我使用的:
I'm learning LINQ to XML and need to find the existence of an element with a particular attribute. At the moment I'm using:
XElement groupCollectionXml = XElement.Parse(groupCollection.Xml);
IEnumerable<XElement> groupFind =
from vw in groupCollectionXml.Elements("Group")
where (string) vw.Attribute("Name") == groupName
select vw;
if (groupFind.Count() == 0)
return false;
else
return true;
我知道有这样做的更简洁的方式,可能使用任何(),但我不知道如何重写使用它的查询。有没有人有一些好的建议?谢谢你。
I know there is a more concise way of doing this, probably using Any(), but I'm not sure how to rewrite the query to use it. Does anyone have some good advice? Thanks.
推荐答案
由于其他两个答案。我结合一体的简洁与他人的正确性,然后搅拌并想出了这个效果很好:
Thanks to the other two answers. I combined the conciseness of one with the correctness of another, then stirred and came up with this which works well:
groupCollectionXml.Elements("Group").Any(
vw => string.Equals(vw.Attribute("Name").Value, groupName, StringComparison.OrdinalIgnoreCase)
);
这篇关于我如何查找属性使用LINQ to XML XML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文