LINQ to XML 选择多个元素?
如何在 LINQ to XML 查询中选择多个元素(具有不同名称)?
我有一个这样的查询:
var elems = from descendant in doc.Descendants("foo")
select descendant;
但我想同时选择 foo 和 bar,有点像这样:
var elems = from descendant in doc.Descendants("foo" || "bar")
select descendant;
但这只是为了说明我想要做什么,我知道这不是正确的语法。我不知道应该如何使用 LINQ to XML 来完成它,那么正确的方法是什么?
How do I select multiple elements (with different names) in a LINQ to XML query?
I have a query like this:
var elems = from descendant in doc.Descendants("foo")
select descendant;
But I want to select both foo and bar, sort of like this:
var elems = from descendant in doc.Descendants("foo" || "bar")
select descendant;
But that is just to illustrate what I want to do, I know this is not correct syntax. I don't know how it should be done with LINQ to XML, so what is the proper way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,一种选择:
Well, one option:
您只能将一个
XName
传递到这些方法中。只需将它们留在那里并执行正常的 LINQ 过滤即可。使用 XPath 是另一种方法。
You can only pass in one
XName
into those methods. Just leave them out there and do normal LINQ filtering.Using an XPath is another way.