[英]How can I use DataSets to store my application data in an XML file?
On my main window I have a DataGridView that I wish to display my data. 在主窗口上,我有一个DataGridView,我希望显示我的数据。 My application allows users to input, change, and delete data. 我的应用程序允许用户输入,更改和删除数据。 I asked my friend the best method of doing this he said storing the information in an XML file. 我问我的朋友最好的方法,他说将信息存储在XML文件中。 So now I am wondering on HOW to use XmlSerializer. 所以现在我想知道如何使用XmlSerializer。 Can I make an XML document or DataSet and give it values, but still be able to read, add, and change those values (via DataGridView)? 我可以制作一个XML文档或DataSet并为其提供值,但仍然能够(通过DataGridView)读取,添加和更改这些值吗? Also, I would like to check if the XML file is created (if it is the first time the application is executed, create the xml; if not, use created xml file). 另外,我想检查是否创建了XML文件(如果是第一次执行应用程序,请创建xml;否则,请使用创建的xml文件)。
Also make sure it's in C#! 还要确保它在C#中!
请参阅有关使用XML作为DataGridView的数据源的问题
Here is an example VB.NET app that opens an XML file by reading it into a DataSet, then assigns the DataTables created in the DataSet as the source for a DataGridView. 这是一个示例VB.NET应用程序,该应用程序通过将XML文件读入DataSet来打开它,然后将在DataSet中创建的DataTable分配为DataGridView的源。 It lets you edit and add rows to the grid view, then saves it back to the XML file: 它允许您编辑并将行添加到网格视图,然后将其保存回XML文件:
http://dot-dash-dot.com/files/wtfxml.zip http://dot-dash-dot.com/files/wtfxml.zip
Depends how much control you want over the XML, but something like this should give you the idea: 取决于您希望对XML进行多少控制,但是类似这样的方法应该可以使您想到:
DataTable dt = new DataTable("MyTable");
dt.Columns.Add(new DataColumn("MyCol1", typeof(string)));
dt.Columns.Add(new DataColumn("MyCol2", typeof(int)));
DataSet ds = new DataSet();
ds.Tables.Add(dt);
dt.Rows.Add("Val1", 5);
dt.Rows.Add("Val2", 6);
ds.WriteXml("data.xml");
DataSet ds2 = new DataSet();
ds2.ReadXml("data.xml");
Not using XmlSerializer (directly), though. 但是,不使用XmlSerializer(直接)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.