<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: database code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 14:36:45 GMT</pubDate>
    <description>DZone Snippets: database code</description>
    <item>
      <title>From DataSet to XmlDocument</title>
      <link>http://snippets.dzone.com/posts/show/3696</link>
      <description>Ever had to take a DataSet from the database and turn it into an Xml Document in .NET? The process is a little messy because there is no direct conversion method. This is because Xml is streamed from the DataSet but the XmlDocument needs a string to load from. Here's my solution:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; // This is the final document&lt;br /&gt;XmlDocument Data = new XmlDocument();&lt;br /&gt;&lt;br /&gt;// Create a string writer that will write the Xml to a string&lt;br /&gt;StringWriter stringWriter = new StringWriter();&lt;br /&gt;&lt;br /&gt;// The Xml Text writer acts as a bridge between the xml stream and the text stream&lt;br /&gt;XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter);&lt;br /&gt;&lt;br /&gt;// Now take the Dataset and extract the Xml from it, it will write to the string writer&lt;br /&gt;content.WriteXml(xmlTextWriter, XmlWriteMode.IgnoreSchema);&lt;br /&gt;&lt;br /&gt;// Write the Xml out to a string&lt;br /&gt;string contentAsXmlString = stringWriter.ToString();&lt;br /&gt;&lt;br /&gt;// load the string of Xml into the document&lt;br /&gt;Data.LoadXml(contentAsXmlString);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 19 Mar 2007 15:58:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3696</guid>
      <author>brainwipe (Rob Lang)</author>
    </item>
  </channel>
</rss>
