tags:

views:

72

answers:

4

i have this code in c#

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

how can i check whether it is returning null?

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

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

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

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.

Mendy
Surprise! Assuming doc is an XmlDocument, there isn't one here :-/
pst