Hi RahulPal,
Will you please let me know how you are binding the gridview using datatable or dataset.
Or you can refer the below code to convert DataTable/DataSet to Xml.
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add("1", "A");
dt.Rows.Add("2", "B");
dt.Rows.Add("3", "C");
DataSet ds = new DataSet();
ds.Tables.Add(dt);
// 1st method using DataSet.
string xml = ds.GetXml();
// 2nd method using DataTable.
string xml1 = GenerateXML(dt);
}
public string GenerateXML(DataTable table)
{
if (table == null)
{
return null;
}
else
{
using (var sw = new StringWriter())
using (var tw = new XmlTextWriter(sw))
{
table.TableName = @"MyTable";
tw.Formatting = Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement(@"data");
((IXmlSerializable)table).WriteXml(tw);
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
sw.Flush();
return sw.ToString();
}
}
}