我正在尝试将dataGridView中的数据序列化,然后将其放入类中,然后写入文件。但是,我遇到了一个问题,序列化程序会将每个对象放在它自己的“文件”中,而不是将所有对象放在一个文件中;当我打开.xml文件时,您可以看到:
<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Id>1</Id>
<Column1>XXX</Column1>
<Column2>YYY</Column2>
</MyClass>
<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Id>2</Id>
<Column1>ZZZ</Column1>
<Column2>AAA</Column2>
</MyClass>
这是我用来序列化的代码。我如何解决这个问题,以便当我打开.xml文件时,我只得到一个文件,而不是像上面那样拆分?我是否必须将所有对象添加到列表中,然后从列表中写入?
private void expXML_Click(object sender, EventArgs e)
{
XmlSerializer ser = new XmlSerializer(typeof(MyClass));
TextWriter writer = new StreamWriter("Filename.XML");
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
MyClass abc = new MyClass(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value), dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Rows[i].Cells[2].Value.ToString());
ser.Serialize(writer, abc);
}
writer.Close();
}
答案 0 :(得分:1)
好吧,我按照建议和我自己的预感;这是经过编辑的代码,它正确地序列化了数据:
private void expXML_Click(object sender, EventArgs e)
{
List<MyClass> newList = new List<MyClass>();
XmlSerializer ser = new XmlSerializer(typeof(List<MyClass>));
TextWriter writer = new StreamWriter("Filename.XML");
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
MyClass abc = new MyClass(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value), dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Rows[i].Cells[2].Value.ToString());
newList.add(abc);
}
ser.Serialize(writer, newList);
writer.Close();
}
答案 1 :(得分:1)
试试这个
private class MyClassList
{
IEnumerable<MyClass> Root{ get; set;}
}
private void expXML_Click(object sender, EventArgs e)
{
var myClassList = new MyClassList();
myClassList.Root = new List<MyClass>();
XmlSerializer ser = new XmlSerializer(typeof(MyClass));
TextWriter writer = new StreamWriter("Filename.XML");
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
MyClass abc = new MyClass(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value), dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Rows[i].Cells[2].Value.ToString());
myClassList.Root.Add(abc);
}
ser.Serialize(writer, myClassList);
writer.Close();
}
答案 2 :(得分:0)
您可以按照以下方式执行此操作:
var ser = new XmlSerializer(typeof(MyClass));
using (var writer = XmlWriter.Create("Filename.XML", new XmlWriterSettings { Indent = true }))
{
writer.WriteStartElement("root");
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
MyClass abc = new MyClass(Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value), dataGridView1.Rows[i].Cells[1].Value.ToString(), dataGridView1.Rows[i].Cells[2].Value.ToString());
ser.Serialize(writer, abc);
}
writer.WriteEndElement();
}
在这里,我们使用XmlWriter
手动创建根元素,允许您使用XmlSerializer
重复编写。
但是,最好使用像List<MyClass>
这样的集合,并且(de)完全序列化它。
另外,我建议你做数据绑定:
var list = new List<MyClass>();
// fill the list
// bind collection to control
dataGridView1.DataSource = list;