Click here to Skip to main content
15,880,392 members
Articles / Web Development / ASP.NET
Article

Export DataSet to XML File

Rate me:
Please Sign up or sign in to vote.
2.35/5 (18 votes)
15 Feb 2008CPOL 117.2K   2.4K   21   13
Export/create Dataset to Xml file.

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)


Written By
Software Developer
India India
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.

Comments and Discussions

 
Questionxmlwrite Pin
Vinay Jade16-Dec-13 2:47
professionalVinay Jade16-Dec-13 2:47 
QuestionExport DataSet to XML File Pin
manoj4achu17-Feb-13 18:58
manoj4achu17-Feb-13 18:58 
Questiongenerating xml from dataSet Pin
Rohith Reddy Vadiyala21-Nov-12 21:35
Rohith Reddy Vadiyala21-Nov-12 21:35 
XML
Hi,
This is Rohith.

When I use dataSet.GetXml()  or dataSet.WriteXml();
It generates the xml but it looks like this---->



Here is the outout....

<?xml version="1.0" standalone="yes"?>
<Soap_x003A_Body>
  <GetTaxInfoByAddress>
    <address>20875 Valley Green Drive</address>
    <city>Cupertino</city>
    <county>USA</county>
    <state>CA</state>
    <zipCode>95014</zipCode>
  </GetTaxInfoByAddress>
  <GetTaxInfoByAddress>
    <address>10100 Mary Avenue</address>
    <city>Cupertino</city>
    <county>USA</county>
    <state>CA</state>
    <zipCode>95014</zipCode>
  </GetTaxInfoByAddress>
  <GetTaxInfoByAddress>
    <address>3101 Bridges St</address>
    <city>Morehead City</city>
    <county>USA</county>
    <state>NC</state>
    <zipCode>28557</zipCode>
  </GetTaxInfoByAddress>
  <GetTaxInfoByAddress>
    <address>19608 Pruneridge Avenue</address>
    <city>Cupertino</city>
    <county>USA</county>
    <state>CA</state>
    <zipCode>95014</zipCode>
  </GetTaxInfoByAddress>
</Soap_x003A_Body>



Here is the code that I have used...
 var ds = objSql.getData();
        ds.DataSetName = "Soap:Body";  //"Soap:Body
        
         ds.Tables[0].TableName = "GetTaxInfoByAddress";
         System.Xml.XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument(ds);

        
         if (ds.Tables[0].Rows.Count > 0)
         {

         ds.WriteXml(Server.MapPath("~/FileTest/Test.xml"));
        
         }




instead if : in Soap:Body  it is displaying _xoo3A_    similarly for white spaces...   pls help me to solve the issue....
<div class="signature">Thanks you,
Rohith</div>

QuestionC# Implementation of Writing Data to XML Format using Dataset Pin
sangram3609-Mar-12 20:25
sangram3609-Mar-12 20:25 
GeneralAll fields in 1 attribute? Pin
Crazy Joe Devola7-Jun-11 14:11
Crazy Joe Devola7-Jun-11 14:11 
GeneralRe: All fields in 1 attribute? Pin
Crazy Joe Devola7-Jun-11 14:29
Crazy Joe Devola7-Jun-11 14:29 
GeneralThanks Pin
jaffrey11015-Feb-11 21:03
jaffrey11015-Feb-11 21:03 
GeneralMy vote of 1 Pin
Enrico Detoma26-Sep-09 2:53
Enrico Detoma26-Sep-09 2:53 
GeneralYOU BAD Pin
MrGoodly15-Sep-08 7:33
MrGoodly15-Sep-08 7:33 
GeneralTrivial ! Pin
kalyankrishna118-Feb-08 16:38
kalyankrishna118-Feb-08 16:38 
GeneralRe: Trivial ! Pin
ZackJones19-Mar-08 6:32
ZackJones19-Mar-08 6:32 
GeneralWelcome To Codeproject !!! Pin
Abhijit Jana16-Feb-08 0:51
professionalAbhijit Jana16-Feb-08 0:51 
GeneralRe: Welcome To Codeproject !!! Pin
damir_tk17-Feb-08 7:38
damir_tk17-Feb-08 7:38 

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

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