Microsoft KB Archive/301271

= How To Save a DataSet Class as XML in .NET Framework SDK =

Article ID: 301271

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework Class Libraries 1.0
 * Microsoft .NET Framework Class Libraries 1.1
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q301271





IN THIS TASK
SUMMARY
 * Requirements
 * How to Save a DataSet as XML
 * Complete Code Listing

REFERENCES



SUMMARY
This document illustrates how to save relational data that is loaded into a DataSet class to a file as Extensible Markup Language (XML). This demonstrates the transition between relationally mapped data and XML data.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * XML terminology
 * XML Schema Definition (XSD)
 * Creating and reading an XML file

back to the top

How to Save a DataSet as XML
 Open Visual Studio .NET. Create a new Visual Basic .NET Console Application. You can go directly to the complete code listing or continue through these steps to build the application.  Use the Imports statement on the Data and Data.SQLClient namespaces so that you are not required to qualify declarations within those namespaces later in your code. You must use the Imports statement prior to any other declarations. Imports System.Data Imports System.Data.SqlClient   Create a function named GetTitleAuthors that retrieves data from the SQL Server Pubs database. This sample assumes that you have SQL Server (with the Pubs database) installed on a local computer. This sample also assumes that you are using Integrated Authentication. Public Function GetTitleAuthors As DataSet Dim MyConnection As SqlConnection = New SqlConnection(&quot;server=(local);database=pubs;Trusted_Connection=yes&quot;) Dim MyCommand1 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from Authors&quot;, MyConnection) Dim MyCommand2 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from Titles&quot;, MyConnection)

Dim DS As New DataSet MyCommand1.Fill(DS, &quot;Authors&quot;) MyCommand2.Fill(DS, &quot;Titles&quot;)

Return DS End Function   In the Main module of Module1, declare a new DataSet, and call the GetTitleAuthors function to retrieve the DataSet: Dim m_SchemaFile As String m_SchemaFile = &quot;testSchema.xml&quot; Dim m_XmlFile As String m_XmlFile = &quot;test.xml&quot; Dim myDataSet as DataSet myDataSet = GetTitleAuthors   To write out the schema that this DataSet created, use the WriteXmlSchema method of the DataSet class: myDataSet.WriteXmlSchema(m_SchemaFile) </li>  To write out the contents of the DataSet as XML, use a file name to call the WriteXml method of the DataSet class: myDataSet.WriteXml(m_XmlFile, XmlWriteMode.IgnoreSchema) </li> Use the schema and the XML to re-create the dataset as necessary.</li> Build your project.</li> Make sure that the SQL Server is running.</li> Run the project, and then test the client-to-server communication.</li> Save and then close the project.</li></ol>

back to the top

Complete Code Listing
Visual Basic .NET Code Imports System.Data Imports System.Data.SqlClient

Module Module1

Public Function GetTitleAuthors As DataSet Dim MyConnection As SqlConnection = New SqlConnection(&quot;server=(local);database=pubs;Trusted_Connection=yes&quot;) Dim MyCommand1 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from Authors&quot;, MyConnection) Dim MyCommand2 As SqlDataAdapter = New SqlDataAdapter(&quot;select * from Titles&quot;, MyConnection)

Dim DS As New DataSet MyCommand1.Fill(DS, &quot;Authors&quot;) MyCommand2.Fill(DS, &quot;Titles&quot;)

Return DS   End Function

Sub Main

Dim m_SchemaFile As String m_SchemaFile = &quot;testSchema.xml&quot; Dim m_XmlFile As String m_XmlFile = &quot;test.xml&quot;

Dim myDataSet As DataSet

myDataSet = GetTitleAuthors myDataSet.WriteXmlSchema(m_SchemaFile) myDataSet.WriteXml(m_XmlFile, XmlWriteMode.IgnoreSchema)

End Sub

End Module back to the top

<div class="references_section">