Avoid XML exceptions selectSingleNode

I have the following question: I have an XML file with some elements that are an answer to a call to some web service. The problem is that I need to load this XML file and select a specific node, but if the websevice returns a response in which the element I'm trying to extract does not exist, my SelectSingleNode function will not throw an exception. I want to control this exception, but I am not trying to catch, possibly with if, something like:

if (xDoc.SelectSingleNode("//Node") == null) etc... 

obviously this is not so, simply because I am asking this question. I hope I get it. Thanks in advance.

+4
source share
1 answer

Close, but I would use: -

  var node = xDoc.SelectSingleNode("//Node"); if (node != null) // go ahead and use node. 
+8
source

All Articles