开发者

XML how to check if node is returning null?

开发者 https://www.devze.com 2022-12-27 12:16 出处:网络
i have this code in c# d开发者_如何学Coc.SelectSingleNode(\"WhoisRecord/registrant/email\").InnerText

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

精彩评论

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