Click here to Skip to main content
6,629,885 members and growing! (20,473 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Export DataSet to XML File

By Prabhash Chandra

Export/create Dataset to Xml file.
C# (C# 2.0, C# 3.0), .NET (.NET 1.1, .NET 2.0, .NET 3.0), ASP.NET, Ajax, Dev
Posted:15 Feb 2008
Views:20,579
Bookmarked:14 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
13 votes for this article.
Popularity: 2.16 Rating: 1.94 out of 5
8 votes, 61.5%
1
1 vote, 7.7%
2

3
1 vote, 7.7%
4
3 votes, 23.1%
5

Introduction

This Article ia about Export DataSet to XMl file.This Code Snippet take a data set from the database and populate it on the .aspx page after clicking the Button. This code create afile on the current location as in this case it will be created in the C:/Inetpub/wwwroot/ExportImport/...this is Useful because it is very simple to understand and write.

Using the code

use this code into your Code behind file

First you should create a connection string in your WEB.CONFIG file.

// web.config setting
add name="ExportImportCS" connectionString="Data Source=BROAD-12\SQLEXPRESS;Initial Catalog=LibSYSDB;Integrated Security=True" providerName="System.Data.SqlClient
// C# Code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml.Serialization;
using System.Xml;
using System.IO;


public partial class ExportToXml : System.Web.UI.Page
{
//Connection setting on .aspx page
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ExportImportCS"].ConnectionString); 
    protected void Page_Load(object sender, EventArgs e)
    {

    }
// Method created for populate the dataset and grid 
    public void ConnectionXML()
    {
        
        SqlCommand command = new SqlCommand();
        command.CommandText = "SELECT * FROM BookIssueDetails";
        command.CommandType = CommandType.Text;
        command.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        da.Fill(ds, "BookIssueDetails");
        if (ds.Tables[0].Rows.Count > 0)
        {
            grdXML.DataSource = ds;
            grdXML.DataBind();
        }
        // Get a StreamWriter object
        StreamWriter xmlDoc = new StreamWriter(Server.MapPath("~/FileTest/Testdo.xml"), false);
       
        // Apply the WriteXml method to write an XML document
         ds.WriteXml(xmlDoc);
         xmlDoc.Close();
     
    }
    //On click of button event
    protected void btnExportToXml_Click(object sender, EventArgs e)
    {
        ConnectionXML();

    }
}
			
		

		Remember to set the .aspx page must download the zip file. 
	

Points of Interest

I hope i tried to explain well. If you like this code please Vote for this Article.... Please..! Thank You

History

I am working as a software Engineer with MNC in INDIA.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Prabhash Chandra


Member
I'm a software Professional having Master's in Computer Application from Dehradoon and master's in Commerce(Accounts)and working as software developer on ASP.NET(C#),MS Commerce Server 2007 and Share Point.
Occupation: Software Developer
Location: India India

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinsupporterEnrico Detoma3:53 26 Sep '09  
GeneralYOU BAD PinmemberMrGoodly8:33 15 Sep '08  
GeneralTrivial ! Pinmemberkalyankrishna117:38 18 Feb '08  
GeneralRe: Trivial ! PinmemberZackJones7:32 19 Mar '08  
GeneralWelcome To Codeproject !!! PinmemberAbhijit Jana1:51 16 Feb '08  
GeneralRe: Welcome To Codeproject !!! Pinmemberdamir_tk8:38 17 Feb '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Feb 2008
Editor:
Copyright 2008 by Prabhash Chandra
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project