XML, how to check if node returns null?

I have this code in C #

doc.SelectSingleNode("WhoisRecord/registrant/email").InnerText 

how can i check if it returns null?

+4
source share
4 answers
 var n = doc.SelectSingleNode("WhoisRecord/registrant/email"); if (n != null) { // here is the check DoSomething(n.InnerText); } 
+7
source

By null do you mean that an element does not exist?

 try { var n = doc.SelectSingleNode("WhoisRecord/registrant/email"); if (n == string.Empty) { // empty value } // has value DoSomething(n.InnerText); } catch (XPathException) { // null value. throw; } 

I'm not sure if this is correct, I need to check it out.

+2
source
 //assuming xd is a System.XML.XMLDocument... XMLNode node = xd.SelectSingleNode("XPath"); if(node == null) { //Your error handling goes here? }else{ // manipulate node.innerText } 
+1
source

Erm ... with the operator != - != null ? I'm not sure what you are asking.

0
source

Source: https://habr.com/ru/post/1306994/


All Articles