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.
Start Microsoft Visual Studio .NET, and create a new Visual Basic Console Application project.
Add the following code to the top of the Code window:
Imports System.DataImports System.Data.SqlClient
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 DataColumnFor Each dc In ds.Tables("Authors").Columns dc.ColumnMapping = MappingType.AttributeNextds.WriteXml("c:\Authors.xml")Console.WriteLine("Completed writing XML file, using a DataSet")Console.Read()
Modify the SqlConnection string as appropriate for your environment.
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.
Open Authors.xml. Notice that all of the columns are created as attributes for each row.