How To Save a DataSet Class as XML in .NET Framework SDK (301271)
The information in this article 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)
- Microsoft Visual Basic .NET (2003)
This article was previously published under Q301271 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("server=(local);database=pubs;Trusted_Connection=yes")
Dim MyCommand1 As SqlDataAdapter = New SqlDataAdapter("select * from Authors", MyConnection)
Dim MyCommand2 As SqlDataAdapter = New SqlDataAdapter("select * from Titles", MyConnection)
Dim DS As New DataSet
MyCommand1.Fill(DS, "Authors")
MyCommand2.Fill(DS, "Titles")
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 = "testSchema.xml"
Dim m_XmlFile As String
m_XmlFile = "test.xml"
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)
- 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)
- Use the schema and the XML to re-create the dataset as
necessary.
- Build your project.
- Make sure that the SQL Server is running.
- Run the project, and then test the client-to-server
communication.
- Save and then close the project.
back to the top
Complete Code ListingVisual Basic .NET Code
Imports System.Data
Imports System.Data.SqlClient
Module Module1
Public Function GetTitleAuthors() As DataSet
Dim MyConnection As SqlConnection = New SqlConnection("server=(local);database=pubs;Trusted_Connection=yes")
Dim MyCommand1 As SqlDataAdapter = New SqlDataAdapter("select * from Authors", MyConnection)
Dim MyCommand2 As SqlDataAdapter = New SqlDataAdapter("select * from Titles", MyConnection)
Dim DS As New DataSet()
MyCommand1.Fill(DS, "Authors")
MyCommand2.Fill(DS, "Titles")
Return DS
End Function
Sub Main()
Dim m_SchemaFile As String
m_SchemaFile = "testSchema.xml"
Dim m_XmlFile As String
m_XmlFile = "test.xml"
Dim myDataSet As DataSet
myDataSet = GetTitleAuthors()
myDataSet.WriteXmlSchema(m_SchemaFile)
myDataSet.WriteXml(m_XmlFile, XmlWriteMode.IgnoreSchema)
End Sub
End Module
back to the top
REFERENCES For more information about the DataSet class, see the "System.Data.DataSet" topic in the Microsoft .NET
Framework Class Library documentation: For more information, see the "XML and the DataSet" topic in the
Microsoft .NET Framework Developer's Guide documentation:
back to the top
Modification Type: | Minor | Last Reviewed: | 7/1/2004 |
---|
Keywords: | kbHOWTOmaster KB301271 kbAudDeveloper |
---|
|