将数据插入XML [英] Insert data into XML

查看:77
本文介绍了将数据插入XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,允许我将数据插入XML文件。插入没问题但是当我想插入新数据时,它会覆盖以前的数据(完全消失)并用新的数据替换它。如何解决这个问题?



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?

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");
    }

推荐答案

参考: c-tutorial-reading-and-writing-xml-files / [ ^ ]


这篇关于将数据插入XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆