6

i have this code in c#

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

how can i check whether it is returning null?

4 Answers 4

9
var n = doc.SelectSingleNode("WhoisRecord/registrant/email");
if (n != null) { // here is the check
  DoSomething(n.InnerText);
}
Sign up to request clarification or add additional context in comments.

Comments

3

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.

1 Comment

Surprise! Assuming doc is an XmlDocument, there isn't one here :-/
2
//assuming xd is a System.XML.XMLDocument...
XMLNode node = xd.SelectSingleNode("XPath");
if(node == null)
{
 //Your error handling goes here?
}else{
 // manipulate node.innerText 
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.