Microsoft KB Archive/301271

From BetaArchive Wiki

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


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

  1. Open Visual Studio .NET.
  2. 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.
  3. 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
                        
  4. 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
                        
  5. 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
                        
  6. To write out the schema that this DataSet created, use the WriteXmlSchema method of the DataSet class:

    myDataSet.WriteXmlSchema(m_SchemaFile)
                        
  7. 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)
                        
  8. Use the schema and the XML to re-create the dataset as necessary.
  9. Build your project.
  10. Make sure that the SQL Server is running.
  11. Run the project, and then test the client-to-server communication.
  12. Save and then close the project.

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("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


Additional query words: dotnet xml

Keywords: kbhowtomaster KB301271