Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have the following codes which allow me to insert data into XML file. The insertion is ok but when i want to insert a new data, it will overwrite the previous data(gone totally) and replace it with the new one. How to solve the problem?

C#
XmlNode xRoot, xNode1, xNode2;
XmlDocument xDoc = new XmlDocument();
XmlDocument result = new XmlDocument();

xRoot = xDoc.CreateElement("FTP_Information");

    xNode1 = xDoc.CreateElement("Details");

    xNode2 = xDoc.CreateElement("FTP_ID");
    xNode2.InnerText = ID;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("FTP_Desc");
    xNode2.InnerText = desciption;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("File_Suffix");
    xNode2.InnerText = suffix;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("File_Name");
    xNode2.InnerText = filename;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("File_Prefix");
    xNode2.InnerText = prefix;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("Host");
    xNode2.InnerText = host;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("Port");
    xNode2.InnerText = port;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("UserID");
    xNode2.InnerText = enUser;
    xNode1.AppendChild(xNode2);

    xNode2 = xDoc.CreateElement("Password");
    xNode2.InnerText = enPassword;
    xNode1.AppendChild(xNode2);

    xRoot.AppendChild(xNode1);

    if (xRoot != null)
    {
        result.LoadXml(xRoot.OuterXml);
        // Save the document to a file and auto-indent the output.
        XmlTextWriter writer = new XmlTextWriter("ftpSetup.xml", null);
        writer.Formatting = Formatting.Indented;
        result.Save(writer);
        MessageBox.Show("Successful");
    }
Posted
Comments
ArunRajendra 28-Mar-14 3:31am    
I tried the posted code in windows application and it created the xml file.
Jamie888 28-Mar-14 4:09am    
yes, the xml file can be created and data can be inserted. But after the first data is inserted, the second data which can be inserted also will overwrite the first data

1 solution

 
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