How to save a dataset as XML
This example illustrates how to use xmldatadocument to save the relational data in the dataset to an XML file. It indicates the conversion from Relational Data to XML data.
|
VB savedatasetxmldata. aspx |
[Running example] | [View Source Code] |
How to save the dataset ing to the subject of the XSD Schema file has generated a relational table for the dataset; now, this example saves these tables as XML data. This is a process of generating and verifying the Hierarchical XML based on the internal XML Schema Definition Language (XSD) architecture.
As shown in the following code, this example first creates the xmldatadocument of 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); ' Load the DataSet with relation data Dim dataset As New DataSet LoadDataSet(dataset) ' Create an XmlDataDocument for the DataSet Dim datadoc As XmlDataDocument = New XmlDataDocument(dataset) |
C # |
VB |
|
This example uses the loaddataset function to create a dataset. To ensure that the dataset is correctly loaded, this example displays the dataset on the screen. Then, this example uses the writexmlschema method of dataset to write the schema created by dataset. To output dataset content as XML, this example uses the file name to call the writexml method of the dataset. Then, this example reads the newly written XML and displays 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); 'Load the DataSet with relation data Dim myDataSet as DataSet = 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 Dim datadoc as XmlDataDocument = new System.Xml.XmlDataDocument(myDataSet) 'Display the XML DisplayXMLData(datadoc) |
C # |
VB |
|
This example writes XML data to the file personpet. xml. Now, you can use xmlreader to read the XML data in xmldatadocument and display the data to display the XML of the relational data. For more information about reading XML documents, see how to read XML from xmldatadocument.
Summary
- Xmldatadocument can be constructed from dataset. It provides XML APIs for relational data.
- By using the XML method of xmldatadocument, you can access the data input through the dataset relational method.
- You can use the writexml method of dataset or the Save method of xmldatadocument to save XML data. The former stores the standardized view of data mapped through the relationship, while the latter stores completely real XML. If you only input data through dataset, these methods are equivalent.