Microsoft Knowledge Base Email Alertz

KBAlertz.com: (311937) - 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...

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]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

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




Article ID: 311937 - Last Review: July 15, 2004 - Revision: 3.2

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

This article was previously published under Q311937

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

Create the Visual C# .NET Sample

  1. Create a new Visual C# Console Application project.
  2. Add the following code to the top of the Code window:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    					
  3. Add the following code in the static void Main(string[] args) section:
    SqlConnection cnPubs = new SqlConnection("Data Source=<servername>;user id=<username>;
                           password=<Password>;Initial Catalog=Pubs;");
    SqlDataAdapter daAuthors = new SqlDataAdapter("Select * from Authors",cnPubs);
    
    DataSet ds = new DataSet();
    cnPubs.Open();
    
    daAuthors.Fill(ds,"Authors");
    DataTable dt ;
    dt = ds.Tables["Authors"];
    
    foreach (DataColumn dc in dt.Columns)
    	{
    	dc.ColumnMapping = MappingType.Attribute;
    	}
    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.

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
Keywords: 
kbhowtomaster kbsqlclient kbsystemdata KB311937
       

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

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please