Szerkesztés

Megosztás a következőn keresztül:


Save a dataset as XML in .NET Framework applications

Note

Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.

Access the XML data in a dataset by calling the available XML methods on the dataset. To save the data in XML format, you can call either the GetXml method or the WriteXml method of a DataSet.

Calling the GetXml method returns a string that contains the data from all data tables in the dataset that's formatted as XML.

Calling the WriteXml method sends the XML-formatted data to a file that you specify.

To save the data in a dataset as XML to a variable

  • The GetXml method returns a String. Declare a variable of type String and assign it the results of the GetXml method.

    string xmlData = northwindDataSet.GetXml();
    

To save the data in a dataset as XML to a file

  • The WriteXml method has several overloads. Declare a variable and assign it a valid path to save the file to. The following code shows how to save the data to a file:

    string filePath = "ENTER A VALID FILEPATH";
    northwindDataSet.WriteXml(filePath);