XML被覆盖,然后重新打开程序

时间:2015-09-25 09:35:33

标签: c#

所以这是我的代码。

public List<Serialization> list = null;
private void UCAPIn_Load(object sender, EventArgs e)
{
    list = new List<Serialization>();

    if (File.Exists("date.XML"))

    {
        var doc = XDocument.Load("data.XML");


        foreach (XElement element in doc.Descendants("Serialization"))
        {

            list.Add(new Serialization()
            { ID = element.Element("ID").Value,
                APIKEY = element.Element("APIKEY").Value,
                VCODE = element.Element("VCODE").Value });
        }
    }
}

public void button1_Click(object sender, EventArgs e)
{
    {
        try
        {
            Serialization info = new Serialization();                    
            info.APIKEY = txtAPI.Text;
            info.VCODE = txtVerC.Text;
            info.ID = Guid.NewGuid().ToString();                                   
            list.Add(info);                    
            Serialization.SaveData(list, "data.XML");
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

private void button2_Click(object sender, EventArgs e)
{
    this.Close();
}

private void whatIsThisToolStripMenuItem_Click(object sender, EventArgs e)
{

}
// end of UCAPIn

public class Serialization
{
    private string id;
    private string APIkey;
    private string VCode;

    public string ID
    {
        get { return id; }
        set { id = value; }
    }

    public string APIKEY
    {
        get { return APIkey; }
        set { APIkey = value; }
    }

    public string VCODE
    {
        get { return VCode; }
        set { VCode = value; }
    }

    public static void SaveData(List<Serialization> list, string Filename)
    {

        File.Delete(Filename);
        XmlSerializer sr = new XmlSerializer(list.GetType());                    
        TextWriter writer = new StreamWriter(Filename, true);            
        sr.Serialize(writer, list); 
        writer.Close();
    }
}

它创建我的XML并添加到它(多个条目)但是当我关闭程序并重新打开它时,在旧数据中添加更多数据就会消失,被新的数据覆盖。

任何人都可以帮我这个吗?

(提前谢谢)

XML Sample(当File.Delete(Filename);被删除。:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSerialization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Serialization>
        <ID>f2310827-93d0-42aa-9bfe-32624ee5f97b</ID>
        <APIKEY>1234</APIKEY>
        <VCODE>1234</VCODE>
    </Serialization>
</ArrayOfSerialization>

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSerialization xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Serialization>
        <ID>df354c83-1ae2-4cfc-b802-c9682f24f3f6</ID>
        <APIKEY>123</APIKEY>
        <VCODE>123</VCODE>
    </Serialization>
</ArrayOfSerialization>

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSerialization xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Serialization>
        <ID>df354c83-1ae2-4cfc-b802-c9682f24f3f6</ID>
        <APIKEY>123</APIKEY>
        <VCODE>123</VCODE>
    </Serialization>
    <Serialization>
        <ID>a8f6737e-6c08-4e7a-b041-c16b502d4a60</ID>
        <APIKEY>1234</APIKEY>
        <VCODE>1234</VCODE>
    </Serialization>
</ArrayOfSerialization>

1 个答案:

答案 0 :(得分:0)

希望此示例代码可以帮助您。

我正在使用与“序列化”相同的属性类

对于反序列化,请使用以下方法:

public List<Serialization> XMLDeserialize()
{
    XmlSerializer deserializer = new XmlSerializer(typeof(List<Serialization>));
    TextReader reader = new StreamReader(xmlPath);
    var XMLDeserialize = (List<Serialization>) deserializer.Deserialize(reader);
    reader.Dispose();
    return XMLDeserialize;
}

要更新xml文件,请使用此功能:

public void UpdateXMLSerialize(List<Serialization> objXmlData)
{
    XmlSerializer serializer = new XmlSerializer(typeof(List<Serialization>));
    using (TextWriter writer = new StreamWriter(xmlPath))
    {
        serializer.Serialize(writer, objXmlData);
    }
}

在点击事件中在xml中添加新数据:

private void Add_Click(object sender, EventArgs e)
{
    List<Serialization> list = new List<Serialization>();

    if (File.Exists(xmlPath))
    {
        list = XMLDeserialize();

        Serialization obj = new Serialization() { ID = "NewData", APIKEY = "NewAPIKey", VCODE = "NewVCode" };
        list.Add(obj);
        UpdateXMLSerialize(list);              
    }
}