How can I check if XElement
has Record & INVID attributes ?
My function returns only single XElement. i.e,
<INVENTORY>
<Record>
<INVID>1315</INVID>
<INVNAME>TEST LOCATIONTEST</INVNAME>
<HOSPNAME>TEST LOCATION</HOSPNAME>
<INVTYPE>CLINICAL</INVTYPE>
<INVDT>2013-09-30T09:30:00</INVDT>
<INVDEF>YES</INVDEF>
<INVACT>YES</INVACT>
<UPDDTTM />
<UPDUSR />
<ENBREF>true</ENBREF>
<INVPWD>101315</INVPWD>
</Record>
</INVENTORY>
XElement xInventory = GetDefaultInventory();
bool hasInventory = xInventory.Elements("INVID").Any(); //What to do here ?
if (hasInventory)
{
//TO DO Some action
}
If you want to check that inventory element has Record
element with INVID
child element, then you can use XPath:
XElement inventory = GetDefaultInventory();
XElement invid = inventory.XPathSelectElement("Record/INVID");
if (invid == null)
// not exist
Or LINQ way:
bool exists = inventory.Elements("Record").Elements("INVID").Any();
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