Hi,
I want to convert the data in datast to xml format. How do i do it?
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, 4:41 AM
Here is your solution
<%@ 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
Manoj SevdaPosted Nov 14, 2011, 6:25 AM
use ds.WriteXml(write xml file path);
Datta KharadPosted Nov 14, 2011, 4:51 AM
You can try this Solution 2:
DataSet ds = new DataSet();
ds.Tables.Add(dt);
XmlElement xE = (XmlElement)Serialize(ds);
string strXml = xE.OuterXml.ToString();
public XmlElement Serialize(object transformObject)
{
XmlElement serializedElement = null;
try
{
MemoryStream memStream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(transformObject.GetType());
serializer.Serialize(memStream, transformObject);
memStream.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(memStream);
serializedElement = xmlDoc.DocumentElement;
}
catch (Exception SerializeException)
{
}
return serializedElement;
}
Datta KharadPosted Nov 14, 2011, 4:47 AM
You can try this code..
DataSet ds = GetDataSet();
String xmlString=String.Empty;
foreach (DataTable dt in ds.Tables)
{
xmlString += ConvertDataTableToXML(dt);
}
public static String ConvertDataTableToXML(DataTable tableToExport)
{
StringBuilder formattedXML = new StringBuilder();
XmlDocument doc = new XmlDocument();
XmlNode node = doc.CreateNode(XmlNodeType.Element, string.Empty, "root", null);
DataColumnCollection dtColumns=tableToExport.Columns;
foreach (DataRow dataItem in tableToExport.Rows)
{
XmlElement element = doc.CreateElement("data");
foreach (DataColumn thisColumn in dtColumns)
{
object value = dataItem[thisColumn];
XmlElement tmp = doc.CreateElement(thisColumn.ColumnName);
if (value != null)
{
tmp.InnerXml = (RegExUtility.HasSpecialCharacter(value.ToString()) ? @" + value.ToString() + "]]>" : value.ToString());
}
else
{
tmp.InnerXml = string.Empty;
}
element.AppendChild(tmp);
}
node.AppendChild(element);
}
doc.AppendChild(node);
return doc.InnerXml; }
Datta KharadPosted Nov 14, 2011, 4:39 AM
Refer this link...
http://www.c-sharpcorner.com/uploadfile/prabhashchandra/export-dataset-data-to-xml-file/