Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am a rookie in coding, I am trying to solve this issue for a while but seems like I couldnt get it correct. I have a user interface, user fills a combobox and a textbox which stands for rating and mailto. After they add the rule to the listview. if they press the save changes button the rules will be saved to the xml file. if they dont, it shouldnt save the rules to xml file. I read the xml document when the form loads and get the rules to the listbox but when I want to delete a rule which is already been created. I can delete it from the listview. Unfortunately the corresponding datas for that rule is still being kept at the xml file. when I run the form again the data that I erased is still at the listbox. can you help me solve that issue. here is the code;
C#
private void WriteXML()
        {
            XmlDocument xmlDocument = new XmlDocument();
            XmlElement settings = xmlDocument.CreateElement("Settings");
            XmlElement item1 = xmlDocument.CreateElement("Timer");
            XmlElement node = xmlDocument.CreateElement("Rule");
            XmlElement node2 = xmlDocument.CreateElement("Rating");
            XmlElement node3 = xmlDocument.CreateElement("Mailto");
            item1.AppendChild(node);
            settings.AppendChild(item1);
            xmlDocument.AppendChild(settings);
            node.AppendChild(node2);
            settings.AppendChild(node);
            xmlDocument.AppendChild(settings);
            node.AppendChild(node3);
            settings.AppendChild(node);
            xmlDocument.AppendChild(settings);
            xmlDocument.Load(@"C:\Users\Orcun Iyigun\Desktop\Projects\Rules.xml");
            XmlNodeList rules = xmlDocument.SelectNodes("/Rules/Rule");
            XmlNodeList mailto = xmlDocument.GetElementsByTagName("Mailto");
            XmlNodeList rating = xmlDocument.GetElementsByTagName("Rating");
            XmlNode rulenode = xmlDocument.SelectNodes("/Rules/Rule")[0];
            XmlNode newnode = node.CloneNode(true);
             //update element values for the new node
            for (int i = 0; i < this.lvRules.Items.Count; i++)
            {
                newnode.SelectSingleNode("Mailto").InnerText = lvRules.Items[i].SubItems[1].Text;
                newnode.SelectSingleNode("Rating").InnerText = lvRules.Items[i].SubItems[0].Text;
            }           
 // append the new node to the document
            xmlDocument.DocumentElement.AppendChild(newnode);
            // update the gridview to refresh display
            XmlNodeList getrules = xmlDocument.GetElementsByTagName("Rule");
            try
            {
                xmlDocument.Save(@"C:\Users\Orcun Iyigun\Desktop\Projects\Rules.xml");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }


[modified: just fixed the pre tags]
Posted
Updated 2-Nov-10 9:13am
v2

1 solution

If the amount of data is not too large and the structure of your XML is suitable, you could load the XML into an in-memory DataSet/DataTable, both of which have ReadXML and WriteXML methods.

You could then bind your ListView to the DataSet/DataTable and save on exit. All changes should be propagated by doing it that way.

Good luck. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900