Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Save a DataSet as XML?

This sample illustrates how to use XmlDataDocument to save relational data from a DataSet to an XML file. This demonstrates the transition from relational data to XML data.

VB SaveDataSetXMLData.aspx
Run Sample View Source

Having built the relational tables for the DataSet in How Do I...Save DataSet Mappings to an XSD schema file?, this sample now saves these tables as XML data. This is the process of generating hierarchical XML based upon and validated against the internally generated XML Schema Definition language (XSD) schema.

As shown in the following code, the sample first creates an XmlDataDocument for the DataSet.

		
// Load the DataSet with relation data
DataSet dataset = new DataSet();
LoadDataSet(dataset);
// Create an XmlDataDocument for the DataSet
XmlDataDocument datadoc = new XmlDataDocument(dataset);
C#

This example creates a DataSet using the LoadDataSet function. To ensure the correct loading of the DataSet, the sample displays the DataSet to the screen. The sample then uses the WriteXmlSchema method of the DataSet to write out the schema created by the DataSet. To output the contents of the DataSet as XML, the sample calls the WriteXml method of the DataSet using a file name. Then, the sample reads in the XML that it just wrote and display the data on the screen.

		
    // Load the DataSet with relation data
    DataSet myDataSet = new DataSet();
    LoadDataSet(myDataSet);

    DisplayTables(myDataSet);

    String xmlFile = Environment.GetEnvironmentVariable("TEMP") + 
        "\\PersonPetCS.xml";
    String schemaFile = Environment.GetEnvironmentVariable("TEMP") +
        "\\PersonPetCS.xsd";

    // Write out schema representation
    myDataSet.WriteXmlSchema(m_SchemaFile);

    // Write out XML data form relational data
    myDataSet.WriteXml(m_XmlFile, XmlWriteMode.IgnoreSchema);

    // Create an XmlDataDocument for the DataSet
    XmlDataDocument datadoc = new XmlDataDocument(myDataSet);

    // Display the XML
    DisplayXMLData(datadoc);
C#

The sample writes the XML data to the file PersonPet.xml. The XML data in XmlDataDocument can now be read with an XmlReader and displayed, showing the XML for the relational data.

Summary

  1. You can construct the XmlDataDocument from a DataSet. This provides an XML API onto the relational data.
  2. You can access data entered through the relational methods of the DataSet by using the XML methods of the XmlDataDocument.
  3. You can save XML data by either using the WriteXml method of the DataSet or the Save method of the XmlDataDocument. The former saves a normalized view of the relationally mapped data, while the latter saves the XML with full fidelity. If the data has only been entered through the DataSet, then these methods are equivalent.



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.