XML如何检查节点是否返回null?

时间:2010-04-15 04:36:53

标签: c# xml

我在c#

中有这段代码
doc.SelectSingleNode("WhoisRecord/registrant/email").InnerText

如何检查它是否返回null?

4 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:3)

null你的意思是该元素不存在?

try
{
    var n = doc.SelectSingleNode("WhoisRecord/registrant/email");
    if (n == string.Empty) {
        // empty value
    }

    // has value
    DoSomething(n.InnerText);
}
catch (XPathException)
{
    // null value.
    throw;
}

我不确定它是否正确,我需要测试它。

答案 2 :(得分:1)

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

答案 3 :(得分:0)

呃......使用!=运算符 - != null?我不确定你在问什么。

相关问题