I am trying to get value from Xml file using xpath. Here is my code:
XElement docQuote = XElement.Parse(Financial);
string result= docQuote.XPathSelectElement("//ns:Quote",nsmgr).ToString(SaveOptions.DisableFormatting);
This is working fine when Quote
Xml node exist in XML file and return value in between Quote
tags. However Quote xml tag not exist in the XMl file it generates and exception.
Object reference not set to an instance of an object.
I have tried to check NULL as below:
if(docQuote.XPathSelectElement("//ns:Quote",nsmgr) != null
)
and
if(docQuote.XPathSelectElement("//ns:Quote",nsmgr) != null).value != null)
However it doesn't avoid the execution when null.
Please help me to avoid execution when Xml tag not exist.