Convert Dataset to XML Convert Dataset to XML asp.net asp.net

Convert Dataset to XML


You can use ds.WriteXml, but that will require you to have a Stream to put the output into. If you want the output in a string, try this extension method:

public static class Extensions{    public static string ToXml(this DataSet ds)    {        using (var memoryStream = new MemoryStream())        {            using (TextWriter streamWriter = new StreamWriter(memoryStream))            {                var xmlSerializer = new XmlSerializer(typeof(DataSet));                xmlSerializer.Serialize(streamWriter, ds);                return Encoding.UTF8.GetString(memoryStream.ToArray());            }        }    }}

USAGE:

var xmlString = ds.ToXml();// ORResponse.Write(ds.ToXml());


Simply use Dataset.getXml():

doc.LoadXml(ds.GetXml());


Write like below code part

DataTable dt = new DataTable("MyData");dt.WriteXml(@Application.StartupPath + "\\DataBaseValues.xml");

Or, You can convert directly the dataSet also as said by Oded like,

private void WriteXmlToFile(DataSet thisDataSet){   if (thisDataSet == null)    {      return;   }   // Create a file name to write to.   string filename = "myXmlDoc.xml";   // Create the FileStream to write with.   System.IO.FileStream myFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create);   // Create an XmlTextWriter with the fileStream.   System.Xml.XmlTextWriter myXmlWriter =    new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);   // Write to the file with the WriteXml method.   thisDataSet.WriteXml(myXmlWriter);      myXmlWriter.Close();}