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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Nov 14, 2011, 4:56 PM EST
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
nishant ranjanRank #4,961 · 317 reputationPosted Nov 14, 2011, 2:16 AM EST
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();
Sam HobbsRank #107 · 20.5K reputationPosted Nov 14, 2011, 2:13 PM EST
TulasiRank #2,544 · 549 reputationPosted Nov 14, 2011, 7:28 PM EST
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.