简体   繁体   English

如何使用数据集将应用程序数据存储在XML文件中?

[英]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.

相关问题 如何使用ASP.NET应用程序中的XML文件中的数据填充我的DropDownList - How I can fill my DropDownList with Data from a XML File in my ASP.NET Application 使用XML存储数据,我如何使用Entity Framework? - Using XML to store data, how can I use Entity Framework? 如何轻松地将C#数据结构(可序列化XML)存储到数据库中? - How can I easily store my C# data structure(XML serialiazable) into a database? 用于存储每个机器应用程序文件(xml)的目录,以便我的所有用户(使用应用程序)都可以对其进行读写 - Directory to store per machine application file (xml) such that my all users(using application) can read/write to it 如何在我的Visual Studio 2012项目中引用XML文件作为项目代码中的数据源? - How can I reference an XML file in my Visual Studio 2012 project as a data source in my project code? 我已经将XML文件反序列化为class.cs,如何在c#ViewModel中使用它 - I have deserialized my XML file into class.cs, how can I use it in c# ViewModel 我应该使用XML在我的C#.Net应用程序中存储配置设置吗? - Should I use XML to store configuration settings in my C#.Net application? 如何使用 LinQ 读取 XML 文件并将其存储到 OODBMS (Siaqodb) 中? - How can I read an XML file and store into a OODBMSs (Siaqodb) with LinQ? 如何在xml元素中存储一串加密数据? - How can I store a string of encrypted data in an xml element? 我想在我的 xml 数据文件中使用 gurmukhi(旁遮普语) - i want to use gurmukhi (punjabi) in my xml data file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM