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>

Natarajamani VenugopalPosted Sep 16, 2010, 10:42 AM
how to Present those using XSLT in an interface? Thanks in advance :)
Natarajamani VenugopalPosted Sep 16, 2010, 10:40 AM
This helped me sir.. Thanks.. :)
anbu kumaranPosted May 18, 2010, 4:08 AM
hi how are you?
Luis BarriosPosted Apr 14, 2010, 3:18 PM
Are you aware of any software that allow you to pull data from SQL tables into a XML format ?
xavier gomesPosted Mar 31, 2010, 7:03 AM
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();
GopakumarPosted Nov 6, 2008, 12:41 AM
I need the code for exporting data from an XML file to a newly created excel sheet using C#.Net