Click here to Skip to main content
Licence CPOL
First Posted 15 Feb 2008
Views 47,466
Downloads 572
Bookmarked 19 times

Export DataSet to XML File

Export/create Dataset to Xml file.
8 votes, 57.1%
1
1 vote, 7.1%
2

3
2 votes, 14.3%
4
3 votes, 21.4%
5
2.00/5 - 14 votes
μ 2.00, σa 3.12 [?]

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

Software Developer

India India

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAll fields in 1 attribute? Pinmemberyaron klodovski15:11 7 Jun '11  
GeneralRe: All fields in 1 attribute? Pinmemberyaron klodovski15:29 7 Jun '11  
GeneralThanks Pinmemberjaffrey11022:03 15 Feb '11  
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  
Trivial or not the article helps me as I had forgotten how to save a dataset as an xml document.
GeneralWelcome To Codeproject !!! PinmemberAbhijit Jana1:51 16 Feb '08  
GeneralRe: Welcome To Codeproject !!! Pinmemberdamir_tk8:38 17 Feb '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 16 Feb 2008
Article Copyright 2008 by Prabhash Chandra
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid