Microsoft KB Archive/311937

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

Article ID: 311937

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q311937



For a Microsoft Visual Basic .NET version of this article, see 310345.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient

IN THIS TASK

 * Summary
 * Requirements
 * Create the Visual C# .NET Sample



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.

back to the top

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 C# .NET syntax
 * Extensible Markup Language (XML)
 * ADO.NET fundamentals and syntax

back to the top

Create the Visual C# .NET Sample
 Create a new Visual C# Console Application project.  Add the following code to the top of the Code window: using System; using System.Data; using System.Data.SqlClient;   Add the following code in the static void Main(string[] args) section: SqlConnection cnPubs = new SqlConnection(&quot;Data Source= ;user id= ;                      password=;Initial Catalog=Pubs;&quot;); SqlDataAdapter daAuthors = new SqlDataAdapter(&quot;Select * from Authors&quot;,cnPubs);

DataSet ds = new DataSet; cnPubs.Open;

daAuthors.Fill(ds,&quot;Authors&quot;); DataTable dt ; dt = ds.Tables[&quot;Authors&quot;];

foreach (DataColumn dc in dt.Columns) {   dc.ColumnMapping = MappingType.Attribute; } ds.WriteXml(@&quot;C:\Authors.xml&quot;); Console.WriteLine(&quot;Completed writing XML file, using a DataSet&quot;); Console.Read;  Modify the SqlConnection string as appropriate for your environment. Press the F5 key to build and run the application. The message &quot;Completed writing XML file, using a DataSet&quot; 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.</li></ol>

back to the top

Additional query words: display output

Keywords: kbhowtomaster kbsqlclient kbsystemdata KB311937

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.