xml - C# check XmlNode value attribute -
i wrote code check if xmlnode has value, when run crash on !=null. strange because solution known.
private static void traversenodes(xmlnodelist nodes) { foreach (xmlnode node in nodes) { if (!node.haschildnodes) { console.writeline(node.name + " " + node.attributes["id"].value); } if (node.attributes["splitcombinationoperator"].value != null) { console.writeline(node.name + " " + node.attributes["splitcombinationoperator"].value); } else { console.writeline(node.name); } traversenodes(node.childnodes); } }
the error following: object reference not set instance of object.
you should check null values this:
node.attributes["splitcombinationoperator"] != null && node.attributes["splitcombinationoperator"].value != null
otherwise nullreferenceexception
when node.attributes["splitcombinationoperator"]
null while trying access value
property.
Comments
Post a Comment