Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » ASP.NET » How to: Generate XML file from database table

How to: Generate XML file from database table

This article shows how we can show our table data in XML file and we can save that file anywhere in our system.

Author Rank :
Page Views : 25589
Downloads : 303
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
GenarteAXMLFileFromDataBase.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

In this article, I am going to show how we can generate a XML file from our Sql Server database. I am going to explain this task with a simple example.

The default.aspx.cs code is:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.IO;

 

public partial class _Default : System.Web.UI.Page

{   

    SqlConnection con;

    SqlCommand cmd = new SqlCommand();

    SqlDataAdapter da = new SqlDataAdapter();

    DataSet ds = new DataSet();

 

    protected void Page_Load(object sender, EventArgs e)

    {

        string XMLFileName = "D:/XmlFileFromDataBaseData";

        string connString = "Data Source=Local; Initial Catalog=northwind; Uid=sa; pwd=";

        string Query = "select * from Employees";

        XMLGenarate(connString, Query, XMLFileName);

        Response.Write(XMLFileName);

    }

 

    public void XMLGenarate(string connstring, string Query, string FileName)

    {

        string XMLFileName = FileName;

        con = new SqlConnection(connstring);

        da.SelectCommand = new SqlCommand(Query, con);

        da.Fill(ds);

        FileStream fs = null;

        fs = new FileStream(XMLFileName + ".xml", FileMode.OpenOrCreate, FileAccess.Write);

        ds.WriteXml(fs);

        fs.Close();

    }  

}


Here in this application, I am using Employees table of northwind DataBase. The location for generated XML file is  "D:/". When user run the application then we can see the XML file has been generated. The output of the XML file will look something like this:



Figure 1: A XML File of a table.
 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Rahul Kumar Saxena
Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books.
(Talabpur*)
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
An alternative approach by Amit On June 19, 2007
Hi, This is really a nice approach. But i will prefer using an alternative simpler approach... strXML = dataset.getxml
Reply | Email | Modify 
=) by Rodrigo On June 20, 2007
Nice piece of code for getting xml right from a db.
Reply | Email | Modify 
Help out with file writing please by Unlink On June 30, 2007
Tks for the brilliant article. Very helpful! I have a question and I hope you can help out. I am trying to write this file to my webserver. But instead it tries to write to the D: folder (as stated in the article). How can I do it to write in the webserver. When running at my localhost it runs fine, but when running in my webhost it gives me an error saying that I do not have permissions since it is trying to access the C: file. Any suggestions. Really appreciate some help. Thank you.
Reply | Email | Modify 
Re: Help out with file writing please by Simon On July 10, 2007

i'dd like a good solution for this aswell

one solution I could think of though was to exclude the D:/ and just keep the rest of the filename, shouldn't this put the file in the same directory as the aspx file?

Reply | Email | Modify 
Helped me a lot!!!!! by Chand On July 9, 2007
Excellent work...Keep it up
Reply | Email | Modify 
Transform from a table to XML file? by Rumba On August 15, 2007
Hello, I am having an exercise about transforming a table to XML file, the table contains the following: Name, First name, Address, Email, Phone N1 P1 A1 E1@email.com 83343 N2 P2 A2 E2@email.com 34343 We consider Email and Phone as attributes, Could you please tell me how to write it in XML? Thanks
Reply | Email | Modify 
For Modification In Tags by Vijay On March 27, 2010
Hi Rahul,
Great Job Dude, your code is really very useful for me. I have some issues like, how can change Tags like <NewDataSet>, <Table> etc, How can I add Attribute in <Table> tag? Because I need more table data in single XML file.
Thanks in Advance,
Regards.
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.