Carl Camera

DataSet to XmlDocument

Here's a useful bit of transforming code that I keep looking up in several of my projects. Many thanks to Milan Negovan for the vast improvement over my initial code.

using System.Data;
using System.Xml;

DataSet ds = MethodReturnsDataSet();

XmlDocument xdoc = new XmlDocument();


Carl, why not xdoc.LoadXml (ds.GetXml ())?

Milan Negovan 14 Nov 2006

Good Question! I suppose the answer is "I never saw GetXml()." I'll change the article text right away.

For the record, here's my initial bloated solution:

using System.IO;
using System.Xml;

DataSet ds = MethodReturnsDataSet();

// export dataset to xml    
MemoryStream ms = new MemoryStream();

// extract string from MemoryStream
ms.Position = 0;
StreamReader sr = new StreamReader(ms,System.Text.Encoding.UTF8);
String strXml = sr.ReadToEnd();

// load the XmlDocument from the string    
XmlDocument xdoc = new XmlDocument();

Thanks, Milan for that one. Now, it appears I need to do some refactoring...

Carl Camera 14 Nov 2006

Just remembered another one: XmlDataDocument. The class is a DataSet wrapper you treat as an XML document.

Milan Negovan 14 Nov 2006