This documentation is archived and is not being maintained.

How to: Save a Dataset as XML 

The XML data in a dataset can be accessed by calling the XML methods available on the dataset. For saving 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 containing the data from all data tables in the dataset formatted as XML.

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

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

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

    string xmlData = northwindDataSet.GetXml();
    
    
    String xmlData = 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, so declare a variable and assign it a valid path to save the file to.

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

See Also

Show: