Hi,
Can you please give simple example for retrieves data from SQL Table and saves it as an XML File?
Thanks
Praveen
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Nov 14, 2011, 9:56 PM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
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;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
void bind()
{
SqlConnection con = new SqlConnection(connStr);
try
{
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "employee");
GridView1.DataSource = ds;
GridView1.DataMember = "employee";
GridView1.DataBind();
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void btn_save_Click(object sender, EventArgs e)
{
bind();
ds.WriteXml("d:\\employee.xml");
Response.Write("Records saved as xml");
}
}
Thanks
If this post helps you mark it as answer
TulasiPosted Nov 15, 2011, 12:28 AM
check the below code .
public void CreateXml()
{
SqlConnection sqlcon = new SqlConnection(connection string);
SqlDataAdapter sqldad = new SqlDataAdapter("select * from emp", sqlcon);
DataSet dsemp = new DataSet();
sqldad.Fill(dsemp, "Employee");
dsemp.Tables[0].WriteXml(Server.MapPath("XML_Files") + "/testxsl.xml");
}
Dataset have a predefined function to save table data as xml file
//specific table is converted into xml file , and it is stored at XML_Files folder
dsemp.Tables[0].WriteXml(Server.MapPath("XML_Files") + "/testxsl.xml");
Please mark the answer as Accepted if it helps you.
Sam HobbsPosted Nov 14, 2011, 7:13 PM
nishant ranjanPosted Nov 14, 2011, 7:16 AM
StringBuilder str = new StringBuilder();
cmd = new SqlCommand()
con.Close();
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
str.Append(string.Format("
str.Append(string.Format("
while (rd.Read())
{
str.Append(string.Format("{0}
}
str.Append(string.Format("{0}
str.Append(string.Format("{0}
if (!Directory.Exists(Server.MapPath("xml")))
{
Directory.CreateDirectory(Server.MapPath("xml"));
}
File.WriteAllText(Server.MapPath("~/xml/") + loginnm + ".xml", str.ToString()); //to write in the xml file
}
else
{
str.Append(string.Format("
str.Append(string.Format("
str.Append(string.Format("No Appointment"));
str.Append(string.Format("
str.Append(string.Format("{0}
if (!Directory.Exists(Server.MapPath("xml")))
{
Directory.CreateDirectory(Server.MapPath("xml"));
}
File.WriteAllText(Server.MapPath("~/xml/") + loginnm + ".xml", str.ToString()); //to write in the xml file
}
con.Close();