Thursday, June 19, 2008

DataSet to XmlDocument and Back

To convert your DataSet into an XmlDocument:
MyDataSet myDataSet = new MyDataSet();
// Add rows to the DataTables in myDataSet

XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.LoadXml(myDataSet.GetXml());
To convert your XmlDocument into a DataSet:
MyDataSet myDataSet = new MyDataSet();

myDataSet.ReadXml(new MemoryStream((new UTF8Encoding()).GetBytes(myXmlDocument.OuterXml)));


More Info:MSDN: XmlDocument.LoadXml Method
MSDN: DataSet.ReadXml Method (Stream)