<p>Here's a quick question I've been banging my head against today.</p>

<p>I'm trying to convert a .Net dataset into an XML stream, transform it with an xsl file in memory, then output the result to a new XML file. </p>

<p>Here's the current solution:</p>

<pre><code>        string transformXML = @"pathToXslDocument";

        XmlDocument originalXml = new XmlDocument();

        XmlDocument transformedXml = new XmlDocument();

        XslCompiledTransform transformer = new XslCompiledTransform();

        DataSet ds = new DataSet();

        string filepath;

        originalXml.LoadXml(ds.GetXml()); //data loaded prior

        StringBuilder sb = new StringBuilder();

        XmlWriter writer = XmlWriter.Create(sb);

        transformer.Load(transformXML);

        transformer.Transform(originalXml, writer); //no need to select the node

        transformedXml.LoadXml(sb.ToString());

        transformedXml.Save(filepath);

        writer.Close();
</code></pre>

<p>Here's the original code:</p>

<pre><code>BufferedStream stream = new BufferedStream(new MemoryStream());

DataSet ds = new DataSet();

da.Fill(ds);

ds.WriteXml(stream);

StreamReader sr = new StreamReader(stream, true);

stream.Position = 0; //I'm not certain if this is necessary, but for the StreamReader to read the text the position must be reset.

XmlReader reader = XmlReader.Create(sr, null);  //Problem is created here, the XmlReader is created with none of the data from the StreamReader

XslCompiledTransform transformer = new XslCompiledTransform();

transformer.Load(@"&lt;path to xsl file&gt;");

transformer.Transform(reader, null, writer); //Exception is thrown here, though the problem originates from the XmlReader.Create(sr, null)
</code></pre>

<p>For some reason in the transformer.Transform method, the reader has no root node, in fact the reader isn't reading anything from the StreamReader.</p>

<p>My questions is what is wrong with this code?  Secondarily, is there a better way to convert/transform/store a dataset into XML?</p>

<p>Edit:  Both answers were helpful and technically aku's was closer.  However I am leaning towards a solution that more closely resembles Longhorn's after trying both solutions.</p>

[{'answer_id': 41012, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 4, 'selected': True, 'text': '<p>I\'m not sure but it seems that you didn\'t reset position in stream before passing it to XmlReader. Try to seek at the beginning of your stream before trying to read from it. Also it may be necessary to close\\flush stream after you wrote some data to it.</p>\n\n<p><strong>EDIT</strong>:</p>\n\n<p>Just tried following code and it worked perfectly:</p>\n\n<pre><code>    BufferedStream stream = new BufferedStream(new MemoryStream());\n    stream.Write(Encoding.ASCII.GetBytes("&lt;xml&gt;foo&lt;/xml&gt;"), 0, "&lt;xml&gt;foo&lt;/xml&gt;".Length);\n    stream.Seek(0, SeekOrigin.Begin);\n    StreamReader sr = new StreamReader(stream);\n    XmlReader reader = XmlReader.Create(sr);\n    while (reader.Read())\n    {\n         Console.WriteLine(reader.Value);\n    }\n    stream.Close();\n</code></pre>\n'}, {'answer_id': 41020, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 2, 'selected': False, 'text': '<p>You must select the root node.  This doesn\'t use Datasets, but I use this function everyday and it works great.</p>\n\n<pre><code>System.Xml.XmlDocument orgDoc = new System.Xml.XmlDocument();\norgDoc.LoadXml(orgXML);\n\n// MUST SELECT THE ROOT NODE\nXmlNode transNode = orgDoc.SelectSingleNode("/");\nSystem.Text.StringBuilder sb = new System.Text.StringBuilder();\nXmlWriter writer = XmlWriter.Create(sb);\n\nSystem.IO.StringReader stream = new System.IO.StringReader(transformXML);\nXmlReader reader = XmlReader.Create(stream);\n\nSystem.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform();\ntrans.Load(reader);\ntrans.Transform(transNode, writer);\n\nXmlDocument doc = new XmlDocument();\ndoc.LoadXml(sb.ToString());\n\nreturn doc;\n</code></pre>\n'}, {'answer_id': 28757385, 'author': 'user1453680', 'author_id': 1453680, 'author_profile': 'https://Stackoverflow.com/users/1453680', 'pm_score': 0, 'selected': False, 'text': '<p>please look it and use..</p>\n\n<pre><code>using (MemoryStream memStream = new MemoryStream())\n            {\n                memStream.Write(Encoding.UTF8.GetBytes(xmlBody), 0, xmlBody.Length);\n                memStream.Seek(0, SeekOrigin.Begin);\n\n                using (StreamReader reader = new StreamReader(memStream))\n                {\n                    // xml reader setting.\n                    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings()\n                    {\n                        IgnoreComments = true,\n                        IgnoreWhitespace = true,\n\n                    };\n\n                    // xml reader create.\n                    using (XmlReader xmlReader = XmlReader.Create(reader, xmlReaderSettings))\n                    {                           \n                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LoginInfo));\n                        myObject = (LoginInfo)xmlSerializer.Deserialize(xmlReader);\n\n                    }\n\n                }         \n            }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/40999', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2916/']