开发者

XML how to check if node is returning null?

i have this code in c#

d开发者_如何学Coc.SelectSingleNode("WhoisRecord/registrant/email").InnerText

how can i check whether it is returning null?


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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜