.net - NullReferenceException When attempting to update XML C# -
this question has answer here:
- what nullreferenceexception, , how fix it? 32 answers
i have windows form application contains list view updated user. when list view updated populates , creates xml items , stores in hidden text box until save request sent.
when save request sent call function writes external config file updated denoted property.
in total i'm updating 7 settings, 1 fails nullreferenceexception when try update list view , save xml
//the values i'm passing in path config file, setting updating (in case requireddocuments), , string value update. public static void updateconfigfiles(string p_spath, string p_ssettingname, string p_svalue) { bool blnapplychanges = false; system.xml.xmldocument xmldoc = new system.xml.xmldocument(); xmldoc.load(p_spath); int icurrentnode = 0; (icurrentnode = 0; icurrentnode <= xmldoc.childnodes[1]["applicationsettings"].childnodes[0].childnodes.count - 1; icurrentnode++) if (xmldoc.childnodes[1]["applicationsettings"].childnodes[0].childnodes[icurrentnode].attributes[0].value.tostring().toupper() == p_ssettingname.toupper()) { //this line exception occurs xmldoc.childnodes[1]["applicationsettings"].childnodes[0].childnodes[icurrentnode].childnodes[0].innertext = p_svalue; blnapplychanges = true; } if (blnapplychanges) { xmldoc.save(p_spath); fixblankxmlvalues(p_spath); } }
i had added watches view values of of different nodes, hadn't come across null value yet, wondering if issue way had structured xml. had thought had been clearing passed in values when rebind listview, turned out not case.
here sample structure of xml redacted information.
<setting name="requireddocuments" serializeas="string"> </setting>
has no childnode 'value' want update.
xmldoc.childnodes[1]["applicationsettings"].childnodes[0] = <project.properties.settings>
xmldoc.childnodes[1]["applicationsettings"].childnodes[0].childnodes[icurrentnode] =<setting name="requireddocuments" serializeas="string">
xmldoc.childnodes[1]["applicationsettings"].childnodes[0].childnodes[icurrentnode].childnodes[0] null.
Comments
Post a Comment