For example for the following XML
例如下面的XML
<Order>
<Phone>1254</Phone>
<City>City1</City>
<State>State</State>
</Order>
I might want to find out whether the XElement contains "City" Node or not.
我可能想知道XElement是否包含“City”节点。
3 个解决方案
#1
50
Just use the other overload for Elements.
只对元素使用另一个重载。
bool hasCity = OrderXml.Elements("City").Any();
#2
4
It's been a while since I did XLinq, but here goes my WAG:
我做XLinq已经有一段时间了,但是我的WAG:
from x in XDocument
where x.Elements("City").Count > 0
select x
;
;
#3
1
David's is the best but if you want you can write your own predicate if you need some custom logic OrderXML.Elements("City").Exists(x=>x.Name =="City")
David's是最好的,但是如果您需要一些自定义逻辑order . xml . elements(“City”). exist (x=>x),您可以编写自己的谓词。名字= =“城市”)
#1
50
Just use the other overload for Elements.
只对元素使用另一个重载。
bool hasCity = OrderXml.Elements("City").Any();
#2
4
It's been a while since I did XLinq, but here goes my WAG:
我做XLinq已经有一段时间了,但是我的WAG:
from x in XDocument
where x.Elements("City").Count > 0
select x
;
;
#3
1
David's is the best but if you want you can write your own predicate if you need some custom logic OrderXML.Elements("City").Exists(x=>x.Name =="City")
David's是最好的,但是如果您需要一些自定义逻辑order . xml . elements(“City”). exist (x=>x),您可以编写自己的谓词。名字= =“城市”)