SIGN UP MEMBER LOGIN:    
ARTICLE

Export Dataset Data To XML File

Posted by Prabhash Chandra Articles | How do I February 27, 2008
This article will provides you how can you export DataSet in Asp.Net 2.0 to an XMl File.
Reader Level:

Introduction:

In this code you have to write the file name with the extension .xml and the code will create the xml file with that name and populate it.

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
{
   
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ExportImportCS"].ConnectionString);
    protected
void Page_Load(object sender, EventArgs e)
}

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 FileStream 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();
}

protected void btnExportToXml_Click(object sender, EventArgs e)
{
   
ConnectionXML();
}

Source code Page View

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportToXml.aspx.cs" Inherits="ExportToXml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 transitional.dtd">

<
html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grdXML" runat="server">
</asp:GridView>
</div>
<asp:Button ID="btnExportToXml" runat="server" OnClick="btnExportToXml_Click" Text="EXPORT XML" />
</form>
</body>
</html>

Login to add your contents and source code to this article
share this article :
post comment
 

how to Present those using XSLT in an interface?
Thanks in advance :)

Posted by Natarajamani Venugopal Sep 16, 2010

This helped me sir..
Thanks.. :)

Posted by Natarajamani Venugopal Sep 16, 2010

hi how are you?

Posted by anbu kumaran May 18, 2010

Are you aware of any software that allow you to pull data from SQL tables into a XML format ?




Posted by Luis Barrios Apr 14, 2010

Hi Prabhash,
I have tried the above code, but I am not getting it to work. Could it be because I have connected to my database using dataconnection instead of having it in App data folder. I have pasted the code and error message that I am getting. Please HELP!
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

public partial class Login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ExportImportCS"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e);
}

public void ConnectionXML()
{
    SqlCommand command = new SqlCommand();
    command.CommandText = "SELECT * FROM tbl_Member";
    command.CommandType = CommandType.Text;
    command.Connection = con;
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    da.Fill(ds, "Member");
    if (ds.Tables[0].Rows.Count > 0)
    {
        grdXML.DataSource = ds;
        grdXML.DataBind();
    }

    // Get a FileStream 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();
}

protected void btnExportToXml_Click(object sender, EventArgs  e)
{
    ConnectionXML();
}

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct

Source Error:


Line 22: }
Line 23:
Line 24: public void ConnectionXML()
Line 25: {
Line 26: SqlCommand command = new SqlCommand();

Posted by xavier gomes Mar 31, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Team Foundation Server Hosting
Become a Sponsor