简体   繁体   English

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

[英]How do you deserialize XML data to a List that was serialized from a List?

I've serialized a bunch of records from a list to an XML file with the following code. 我已使用以下代码将一堆记录从列表序列化为XML文件。 This works very well, and I have a really nice XML file that stores my data when I close my program. 这非常有效,并且我有一个非常不错的XML文件,当我关闭程序时,该文件可以存储数据。

How do I now read that data back into my list when the program opens? 程序打开后,我现在如何将这些数据读回到列表中? I can't seem to figure out how to cycle through the records in the file and store them in my list of records. 我似乎无法弄清楚如何循环浏览文件中的记录并将它们存储在我的记录列表中。

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

Below is as far as I got: 以下是我得到的:

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

For reference, the data stored in the XML file came from a list with the following format: 作为参考,存储在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; }
}

I also tried serializing the whole list as an object instead of doing it record by record, but that didn't work to well either. 我还尝试将整个列表序列化为一个对象,而不是逐个记录地进行序列化,但这也不能很好地工作。

EDIT: The code shown below does work. 编辑:下面显示的代码可以正常工作。 The reason it failed when I tried it was I started by trying to deserialize a list that was serialized record by record. 尝试失败的原因是,我尝试对每个记录进行序列化的列表反序列化。 Clearly the two approaches aren't compatible the code below is the correct approach to this. 显然这两种方法不兼容,下面的代码是正确的方法。

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

Can you try this 你可以试试这个吗

     // 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();

check here for details 在这里查看详细信息

XML Serialization and Deserialization: Part-1 XML序列化和反序列化:第1部分

XML Serialization and Deserialization: Part-2 XML序列化和反序列化:第2部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM