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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -