How To Render DataTable Columns as XML Attributes Instead of Elements by Using Visual Basic .NET

Article translations Article translations
Article ID: 310345 - View products that this article applies to.
This article was previously published under Q310345
Expand all | Collapse all

On This Page

SUMMARY

This article demonstrates how to render DataTable columns as Extensible Markup Language (XML) attributes. A DataTable represents one table of in-memory relational data. You can create a DataTable and use it independently, or other Microsoft .NET Framework objects can use the DataTable, most commonly as a member of a DataSet object.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows XP, Windows 2000, or Windows NT 4.0 Service Pack 6a
  • Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Visual Basic .NET syntax
  • Extensible Markup Language (XML)
  • ADO.NET fundamentals and syntax

Steps to Create Visual Basic .NET Sample

  1. Start Microsoft Visual Studio .NET, and create a new Visual Basic Console Application project.
  2. Add the following code to the top of the Code window:
    Imports System.Data
    Imports System.Data.SqlClient
    					
  3. Add the following code in the Sub Main procedure:
    Dim cnPubs As New SqlConnection("Data Source=<servername>;user id=<username>;" & _
                                    "password=<password>;Initial Catalog=Pubs;")
    Dim daAuthors As New SqlDataAdapter("Select * from Authors", cnPubs)
    
    Dim ds As New DataSet()
    cnPubs.Open()
    daAuthors.Fill(ds, "Authors")
    
    Dim dc As DataColumn
    For Each dc In ds.Tables("Authors").Columns
            dc.ColumnMapping = MappingType.Attribute
    Next
    
    ds.WriteXml("c:\Authors.xml")
    
    Console.WriteLine("Completed writing XML file, using a DataSet")
    Console.Read()
    					
  4. Modify the SqlConnection string as appropriate for your environment.
  5. Press the F5 key to build and run the application. The message "Completed writing XML file, using a DataSet" appears in the Console window. Notice that the Authors.xml file is created in the specified location.
  6. Open Authors.xml. Notice that all of the columns are created as attributes for each row.

Properties

Article ID: 310345 - Last Review: July 14, 2004 - Revision: 2.3
APPLIES TO
  • Microsoft ADO.NET 1.1
  • Microsoft ADO.NET 1.0
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbsystemdata KB310345

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com