我不知道为什么我在这方面遇到这么多麻烦,但我希望有人能让我指出正确的方向。
我有这几行代码:
var xDoc = new XmlDocument();
xDoc.LoadXml(xelementVar.ToString());
if (xDoc.ChildNodes[0].HasChildNodes)
{
for (int i = 0; i < xDoc.ChildNodes[0].ChildNodes.Count; i++)
{
var sFormatId = xDoc.ChildNodes[0].ChildNodes[i].Attributes["formatID"].Value;
// Do some stuff
}
// Do some more stuff
}
问题是xDoc
我得到的并不总是有formatID
节点,所以我最终得到一个空引用异常,尽管 99% 的时间它工作得很好。
我的问题 :
formatID
在尝试读取节点之前如何检查节点是否存在Value
?