Search This Blog

Friday, July 24, 2009

Convert DataTable to xml the easy way

If you want to convert your data table to xml - here is the function you need:

private string ConvertTableToXml(DataTable table)
{
StringWriter sw = new StringWriter();

table.WriteXml(sw, XmlWriteMode.IgnoreSchema);

return sw.ToString();

}

and if you want to sort your data table before convert it to xml , here is the code snippets

myDatatable.DefaultView.Sort = "MaxPrice DESC";
string mystr = ConvertTableToXml(myDatatable.DefaultView.ToTable());
enjoy
Yaniv

No comments: