title: How to Convert a StreamReader into an XMLReader object in .Net 2.0/C#
body: <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>