Alex Gordon
Alex Gordon

Reputation: 60731

XML how to check if node is returning null?

i have this code in c#

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

how can i check whether it is returning null?

Upvotes: 6

Views: 27588

Answers (4)

Fitzchak Yitzchaki
Fitzchak Yitzchaki

Reputation: 9163

By null do you mean that the element doesn't 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 don't sure that it is correct, I need to test it.

Upvotes: 3

Warty
Warty

Reputation: 7395

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

Upvotes: 2

EMP
EMP

Reputation: 61971

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

Upvotes: 0

user166390
user166390

Reputation:

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

Upvotes: 9

Related Questions