Sumit Kumawat

Sumit Kumawat

  • NA
  • 454
  • 413.6k

How to add new data in existing xml file?

Dec 29 2012 4:27 AM

Hi all ,

i want to save different user name and password in  single XML file .

for this i wrote this code on save button :-

 

     string filename = "E:\\firstXML.xml";
                        XmlDocument xmlDoc = new XmlDocument();
                        XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                        xmlWriter.Formatting = Formatting.Indented;
                        xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                        xmlWriter.WriteStartElement("Credential");
                        xmlWriter.Close();
                        xmlDoc.Load(filename);
                        XmlNode root = xmlDoc.DocumentElement;
                        XmlElement childNode1 = xmlDoc.CreateElement("UserName");
                        XmlElement childNode2 = xmlDoc.CreateElement("Password"); 

root.AppendChild(childNode1);
                          childNode1.InnerText = tbName.Text;

  root.AppendChild(childNode2);
                      childNode2.InnerText = tbPassword.Text;

  xmlDoc.Save(filename);

USING THIS XML IS CREATING BUT NEXT TIME IF I AM INSERTING DIFFERENT USER NAME AND PASSWORD IT IS REPLACING MY PREVIOUS DATA. HOW TO ADD ALL USER NAME AND PASSWORD INTO SINGLE XML FILE PLEASE HELP.. 



Answers (6)