将列表列表导出到 XML,反之亦然
Export list of list to XML and vice-versa
我正在尝试为我的 Visual C# 程序实现一个 "Open/Save" 函数。我需要保存的对象是列表列表。更具体地说,它是一个 List<List<Components>>
,其中 Components
是我的习惯 class。我设法导出它并使用 XML reader 读取我的变量,但是当我将它导入回程序时,我得到了错误的变量。比如导出前的列表容量是2,导入后变成了4,有帮助吗?
如果您可以建议 "Open/Save" 功能的另一种方式,请随意。我只需要将自定义 class 的列表存储和恢复到一个文件中。
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);
}
}
此解决方案的缺点是您无法查看文件和浏览数据。进一步的改进是使用泛型,这样您就可以 serialize/deserialize 任何类型的数据。
不要忘记用 [Serializable]
属性标记您要保存的 class
我正在尝试为我的 Visual C# 程序实现一个 "Open/Save" 函数。我需要保存的对象是列表列表。更具体地说,它是一个 List<List<Components>>
,其中 Components
是我的习惯 class。我设法导出它并使用 XML reader 读取我的变量,但是当我将它导入回程序时,我得到了错误的变量。比如导出前的列表容量是2,导入后变成了4,有帮助吗?
如果您可以建议 "Open/Save" 功能的另一种方式,请随意。我只需要将自定义 class 的列表存储和恢复到一个文件中。
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);
}
}
此解决方案的缺点是您无法查看文件和浏览数据。进一步的改进是使用泛型,这样您就可以 serialize/deserialize 任何类型的数据。
不要忘记用 [Serializable]