Microsoft Knowledge Base Email Alertz

This document illustrates how to save relational data that is loaded into a

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 301271 - Last Review: March 29, 2007 - Revision: 4.4

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

This article was previously published under Q301271

On This Page

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.

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

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.

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
				

REFERENCES

For more information about the DataSet class, see the "System.Data.DataSet" topic in the Microsoft .NET Framework Class Library documentation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp)
For more information, see the "XML and the DataSet" topic in the Microsoft .NET Framework Developer's Guide documentation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconXMLDataSet.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconXMLDataSet.asp)

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
Keywords: 
kbhowtomaster KB301271
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate