For example for the following 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.
If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as string or Nullable<T> of Int32 . If the attribute does not exist, then the nullable type is set to null.
The simplest way to search XML is to use the Linq-to-XML API in . NET (in System. Xml.
Just use the other overload for Elements.
bool hasCity = OrderXml.Elements("City").Any();
It's been a while since I did XLinq, but here goes my WAG:
from x in XDocument where x.Elements("City").Count > 0 select x
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With