如何将XML数据反序列化为从List序列化的List

本文关键字:List 序列化 反序列化 XML 数据 | 更新日期: 2023-09-27 18:19:07

我用以下代码将一堆记录从列表序列化到XML文件。这工作得非常好,当我关闭程序时,我有一个非常好的XML文件来存储数据。

当程序打开时,我现在如何将该数据读取回我的列表?我似乎不知道如何循环遍历文件中的记录并将它们存储在我的记录列表中。

    private void WriteXML()
    {
        try
        {
            System.Xml.Serialization.XmlSerializer XMLwriter = new System.Xml.Serialization.XmlSerializer(typeof(callsignRecord));
            System.IO.StreamWriter XMLfile = new System.IO.StreamWriter("Known Callsigns.xml");
            foreach (callsignRecord callsign in Callsigns)
            {
                XMLwriter.Serialize(XMLfile, callsign);
            }
            XMLfile.Close();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

下面是我所看到的:

    private void ReadXML()
    {
        try
        {
            System.Xml.Serialization.XmlSerializer XMLreader = new System.Xml.Serialization.XmlSerializer(typeof(callsignRecord));
            System.IO.StreamReader XMLfile = new System.IO.StreamReader("Known Callsigns.xml");
            while(!XMLfile.EndOfStream)
            {
                // Okay, great I can Deseralize the file, but how do the records go from the file to the List with this?
                XMLreader.Deserialize(XMLfile);
            }
            XMLfile.Close();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

作为参考,存储在XML文件中的数据来自具有以下格式的列表:

public class callsignRecord
{
    public string User { get; set; }
    public List<AliasRecord> AliasRecords;
}
public class AliasRecord
{
    public string Alias { get; set; }
    public string Number { get; set; }
}

我还尝试将整个列表序列化为一个对象,而不是逐个记录,但这也不太好。

编辑:下面显示的代码可以工作。当我尝试它失败的原因是,我开始尝试反序列化一个记录序列化的列表。显然,这两种方法是不兼容的,下面的代码是正确的方法。

    private void WriteXML()
    {
        try
        {
            var XMLwriter = new XmlSerializer(typeof(List<callsignRecord>));
            System.IO.StreamWriter XMLfile = new System.IO.StreamWriter("Known Callsigns.xml");
            XMLwriter.Serialize(XMLfile, Callsigns);
            XMLfile.Close();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }
    private void ReadXML()
    {
        System.IO.StreamReader XMLfile;
        try
        {
            var XMLreader = new XmlSerializer(typeof(List<callsignRecord>));
            XMLfile = new System.IO.StreamReader("Known Callsigns.xml");
            Callsigns = (List<callsignRecord>)XMLreader.Deserialize(XMLfile);
            XMLfile.Close();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            //XMLfile.Close();
        }
    }

如何将XML数据反序列化为从List序列化的List

你可以试试这个吗

     // Create an instance of CallsignRecordclass.
     CallsignRecord serializeObject = new CallsignRecord();
     // Create an instance of new TextReader.
     TextReadertxtReader = new StreamReader(@”C:'Callsigns.xml”);
     // Create and instance of XmlSerializer class.
     XmlSerializerxmlSerializer = new XmlSerializer(typeof(CallsignRecord));
     // Deserialize from the StreamReader.
     serializeObject = (CallsignRecord)xmlSerializer.Deserialize(txtReader);
     // Close the stream reader
     txtReader.Close();

查看详情

XML序列化和反序列化:第1部分

XML序列化和反序列化:第2部分

相关文章: