簡體   English   中英

將列表導出到XML,反之亦然

[英]Export list of list to XML and vice-versa

我正在嘗試為我的Visual C#程序實現“打開/保存”功能。 我需要保存的對象是列表列表。 更具體地說,它是List<List<Components>> ,其中Components是我的自定義類。 我設法導出它,並使用XML閱讀器讀取變量,但是當我將其重新導入程序時,我得到了錯誤的變量。 例如,導出之前的列表容量為2。導入之后,列表容量變為4。有什么幫助嗎?
如果您可以建議另一種“打開/保存”功能,請放心。 我只需要將自定義類列表的列表存儲並還原到文件中。

public void open_click(object send, EventArgs e)
        {
            XmlSerializer xml = new XmlSerializer(typeof(List<List<Components>>));
            OpenFileDialog oDialog = new OpenFileDialog();

            oDialog.Filter = "XML|*.xml";

            if (oDialog.ShowDialog() == DialogResult.OK)
            {
                Variables.compBuffer = new List<List<Components>>();
                using (FileStream s = File.OpenRead(oDialog.FileName))
                using (StreamReader sw = new StreamReader(s))
                {
                    Variables.compBuffer = (List<List<Components>>)xml.Deserialize(s);
                }
            }
        }    

//

public void saveAs_click(object send, EventArgs e)
{
    XmlSerializer xml = new XmlSerializer(typeof(List<List<Components>>));
    SaveFileDialog sDialog = new SaveFileDialog();

    sDialog.FileName = "myLadder";
    sDialog.Filter = "XML|*.xml";
    sDialog.OverwritePrompt = true;

    if (sDialog.ShowDialog() == DialogResult.OK)
    {
        using (FileStream s = File.OpenWrite(sDialog.FileName))
        using (StreamWriter sw = new StreamWriter(s))
        {
            xml.Serialize(s, Variables.compBuffer);
        }
    }
}    

//

    public class Components
    {
        //Private variables
        private string _type = "empty";
        private string _name = "";
        private int _time = 0;    //If it's a "timer" (in ms)
        private int _index = -1;
        private string _comment = "";
        private bool _output = false;

        public Components() { }

        public string Type
        {
            get { return this._type; }
            set { this._type = value; }
        }
        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }
        public int Time
        {
            get { return this._time; }
            set { this._time = value; }
        }
        public int Index
        {
            get { return this._index; }
            set { this._index = value; }
        }
        public string Comment
        {
            get { return this._comment; }
            set { this._comment = value; }
        }
        public bool Output
        {
            get { return this._output; }
            set { this._output = value; }
        }

        public void reset()
        {
            _type = "empty";
            _name = "";
            _time = 0;               //If it's a "timer" (in ms)
            _index = -1;
            _comment = "";
            _output = false;
        }
    }
}

由於您沒有考慮xml特定的解決方案,因此我只將其作為二進制表示形式序列化到文件中,然后將其還原:

    using System.IO;

    [Serializable]    
    public class Components
    {
       ...
    }

    var components = new List<Components>();
    string pathToFile = @"c:\dev\components.bin";

    SerializeFile(pathToFile, components);
    var fetchComponents = DeserializeFile(pathToFile);

    private void SerializeFile(string file, IList<Components> data)
    {
        using (Stream stream = File.Open(file, FileMode.Create))
        {
           var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
           formatter.Serialize(stream, data);
        }
    }

    private IList<Components> DeserializeFile(string file)
    {
        using (Stream stream = File.Open(file, FileMode.Create))
        {
            var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            return (List<Components>)formatter.Deserialize(stream);
        }
    }

該解決方案的缺點是您無法查看文件並瀏覽數據。 進一步的改進將是使用泛型,以便您可以序列化/反序列化任何類型的數據。

不要忘記使用[Serializable]屬性標記要保存的班級

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM