Save a DataSet as XML

2010-08-28 10:48:35來源:西部e網(wǎng)作者:

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);

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);

 // 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);


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. For more information about reading XML documents, see How do I...Read XML from an XmlDataDocument?.

 

關(guān)鍵詞:DataSetc#

贊助商鏈接: