Save a dataset as XML


This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here

The XML data in a dataset can be accessed 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.This means that you declare a variable of type String and assign it the results of the GetXml method.

    string xmlData = northwindDataSet.GetXml();
    Dim xmlData As String = NorthwindDataSet.GetXml()

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

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

    string filePath = "ENTER A VALID FILEPATH";
    Dim filePath As String = "ENTER A VALID FILEPATH"

See Also

Save data back to the database