0

I currently have a windows form application which contains a list view that is updated by the user. When the list view is updated it also populates and creates XML for the items and stores it in a hidden text box until a save request is sent.

When a save request is sent I call a function which writes to my external Config file to updated the denoted property.

In total I'm updating 7 settings, but the only one that fails with a NullReferenceException is when I try to update the list view and save the XML

//The values I'm passing in are the path to my config file, the setting I am updating (in this case would be RequiredDocuments), and the string value to 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;

    for (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 is where the 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 to view the values of all of the different nodes, hadn't come across a null value yet, so I am wondering if it is an issue with the way I had structured my XML. Initially I had thought I had been clearing the passed in values when I rebind my ListView, but that turned out to not be the case.

Here is a sample structure of the XML with some redacted information.

confusedandamused
  • 746
  • 2
  • 8
  • 28
  • Read the answer on the dupe, and be confused no more. –  Sep 07 '16 at 14:34
  • @Will I understand why it's happening and what an NullReferenceException ~is~ but this question is specific to XML, and marking this question as duplicate isn't very helpful. I had previously referenced that answer as well in the past - thank you for posting it anyways. – confusedandamused Sep 07 '16 at 14:42
  • There is nothing in your question that indicates 1) you have debugged this 2) you have located what code is throwing a NRE and 3) why you cannot fix the exception. The duplicate question guides you through 1 and 2, which you really need to do before asking for help with 3. If you can [edit] to include 1, 2 and 3, please do, and @ me and I'll gladly reopen. –  Sep 07 '16 at 14:47
  • @Will Edited original 1. I'm not sure how to best explain this other then stepping through and explaining previous methods to try to find the null value - I added some background thoughts to original. 2. Denoted with a comment in original 3. Relates to 1's background information since I was unsure of the source. I have gone ahead and marked an answer as correct as it pointed out the issue with the structure of the XML. – confusedandamused Sep 07 '16 at 14:56
  • @Jamiec Have you not been following along to the conversation above? – confusedandamused Sep 07 '16 at 15:08
  • @confusedandamused yes, and now that you have an answer to your *specific* question (you green ticked the answer!), lets help everyone else with a `NullReferenceException` find the canonical answer. (BTW, I dont believe you had a specific question not answered by the dupe, but thats just my opinion) – Jamiec Sep 07 '16 at 15:12
  • @Jamiec No worries I'm still pretty new to SO and just trying to figure out how everything works :) – confusedandamused Sep 07 '16 at 15:50
  • @confusedandamused - no problem. And Welcome :) – Jamiec Sep 07 '16 at 16:07

1 Answers1

0
<setting name="RequiredDocuments" serializeAs="String">     
</setting>

has no childNode 'Value' which you want to 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] is NULL.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
akhileshcoer
  • 162
  • 10
  • So essentially the node I'm trying to update has no child nodes - so it's null? So adding a child node to So the solution would be – confusedandamused Sep 07 '16 at 14:34
  • @confusedandamused: no. Fix your code. Do not add random XML elements somewhere. – Thomas Weller Sep 07 '16 at 14:36
  • @Thomas By your suggestion I would be searching for... xmlDoc.ChildNodes[1]["applicationSettings"].ChildNodes[0].ChildNodes[iCurrentNode] = And adding a new element inside of that named "value" to match the others, and then adding another new element inside of that. Which is good, but since I'm already creating the XML for the new elements elsewhere I feel like that is an extra step. – confusedandamused Sep 07 '16 at 14:39