![](/img/trans.png)
[英]SelectSingleNode returning null for known good xml node path using XPath
[英]XML how to check if node is returning null?
我在c#中有這段代碼
doc.SelectSingleNode("WhoisRecord/registrant/email").InnerText
我怎樣才能檢查它是否返回null?
var n = doc.SelectSingleNode("WhoisRecord/registrant/email");
if (n != null) { // here is the check
DoSomething(n.InnerText);
}
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;
}
我不確定它是否正確,我需要測試它。
//assuming xd is a System.XML.XMLDocument...
XMLNode node = xd.SelectSingleNode("XPath");
if(node == null)
{
//Your error handling goes here?
}else{
// manipulate node.innerText
}
呃...用!=
運算符 - != null
? 我不確定你究竟在問什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.