Friday 5 February 2016

How to convert datatable to xml string in c# ?




Here I wrote a Method by which we can convert a DataTable into XML format. Method ConvertDatatableToXML Receive a parameter of Datatable type and after converting to XMl its return a string.


// By using this method we can convert datatable to xml
public string ConvertDatatableToXML(DataTable dt)
{
MemoryStream str = new MemoryStream();
dt.WriteXml(str, true);
str.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(str);
string xmlstr;
xmlstr = sr.ReadToEnd();
return (xmlstr);

}




Its a very frequently used function in c#. I hope its very useful to readers. Plaese comments your questions and feedback. thank you.

No comments:

Post a Comment