简体   繁体   中英

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. 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. So now I am wondering on HOW to use 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)? 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).


Also make sure it's in 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. It lets you edit and add rows to the grid view, then saves it back to the XML file:

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:

    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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