Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid exception on XML selectSingleNode function

Tags:

c#

.net

xml

i have the following question: I have a XML file with some elements that are the response of the invocation of some webservice. The problem is that i need to load that XML file and select a specific node, but, if the websevice returns a response where the element i'm trying to extract not exist, my function SelectSingleNode will fail producing an exception. I want to control that exception but with no try catch, maybe with an if, something like:

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

obviously it doesn't work thats easy, so thats why i'm posting this question. Hope i made myself clear. Thanks in advance.

like image 515
lidermin Avatar asked Feb 27 '23 22:02

lidermin


1 Answers

Close but I would use:-

 var node = xDoc.SelectSingleNode("//Node");
 if (node != null) // go ahead and use node.
like image 120
AnthonyWJones Avatar answered Mar 07 '23 22:03

AnthonyWJones