Click here to Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralMy vote of 1
Enrico Detoma
3:53 26 Sep '09  
Trivial
GeneralYOU BAD
MrGoodly
8:33 15 Sep '08  
YOU ARE A BAN SHOD AND A MA SHOD!
GeneralTrivial !
kalyankrishna1
17:38 18 Feb '08  
C'mon find something to really work on !
If ppl started posting such trivial stuff like DataSet.WriteXml(), we'd have a 1000 new articles everyday here..

Regards,
Kalyan Krishna

GeneralRe: Trivial !
ZackJones
7: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 !!!
Abhijit Jana
1:51 16 Feb '08  
Good Start !!!
please give some Technical description about your article . then post the code !
and Check the formating !!!! MAximum part are going out of screen !!!

4 from me Smile
Welcome Again !!!!

Best Regards
-----------------
Abhijit Jana
Microsoft Certified Professional
"Success is Journey it's not a destination"


GeneralRe: Welcome To Codeproject !!!
damir_tk
8:38 17 Feb '08  
Wow...this is indeed a very good start. This is brilliant actually, I didn't know you can have a data set and go WriteXml with it, this solves so many issues I had before.
Can't wait your next article, and once again kudos for all this great code that comes from India.
Cheers.


Last Updated 16 Feb 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010