Thursday, March 31, 2011

XML XSL Transform From Stream

Hi I have a memory stream in which I use XMLTextWriter to write out some XML. What I want to do is transform this XML using the XSL.transform feature. The thing is I don't want to create a temp. XML file, I want to somehow use the stream to Transform it into the XSL. Thank you for any suggestions.

From stackoverflow
  • Just use an XmlReader and pass it to the Transform method. You'll need to seek to the start of the stream first.

      stream.Seek(0, SeekOrigin.Begin);
      XmlReader reader = XmlReader.Create(stream, settings);
      XslCompiledTransform transform = new XslCompiledTransform();
      transform.Load(...load your transform...);
      transform.Transform(reader, resultsWriter);
    

    I've obviously removed certain elements here to simplify the code, but you should get the idea.

    morechilli : Worth noting that your transform can be loaded from many things including a stream by the above Load method. You should never need to drop down to a file.
    Jeff Yates : Yup, the Load method has many useful overrides.

0 comments:

Post a Comment