How to xsltransform to an xml string in memory?

Last post 07-22-2005 11:04 AM by rmprimo. 6 replies.

Sort Posts:

  • How to xsltransform to an xml string in memory?

    07-20-2005, 9:54 AM
    • Contributor
      3,639 point Contributor
    • rmprimo
    • Member since 02-24-2003, 7:25 AM
    • Posts 731
    I have a custom control which can consume either an xml file or an xml string. They are both exposed as methods and consructor overloads.

    MyCustomControl mc =new MyCustomControl("File.xml");
    MyCustomControl mc =new MyCustomControl(string xmlString);

    This works, however the runtime needs to have write permissions and I don't really want to keep this file anyway, just use it at runtime.

    //or the v1.1 XslTransform class-same issue
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load(Server.MapPath(
    "StyleSheet.xslt"));
    xslt.Transform(Server.MapPath(
    "Source.xml"), Server.MapPath("Result.xml"));

    MyCustomControl mc =new MyCustomControl("Result.xml");
    ...

    I would much rather be able to:

    MyCustomControl mc =new MyCustomControl(resultXmlString);
    ...


    Any tip would be greatly appreciated.

    TIA

    Rob





    Regards,

    Rob
  • Re: How to xsltransform to an xml string in memory?

    07-20-2005, 11:09 AM
    • Participant
      860 point Participant
    • JasonFollas
    • Member since 06-02-2005, 6:13 PM
    • 41.501000, -83.718000
    • Posts 170
    Look at the sample code in this thread: http://forums.asp.net/977351/ShowPost.aspx

    The Transform() method is overloaded, and the code in that thread shows one way to invoke it and get a XmlReader returned (which can then be used to get the string).
  • Re: How to xsltransform to an xml string in memory?

    07-20-2005, 3:14 PM
    • Contributor
      3,639 point Contributor
    • rmprimo
    • Member since 02-24-2003, 7:25 AM
    • Posts 731
    Thank you. That sure worked.

    For reference, here is the snippet that did it:

    //using a different overload that returns an XmlReader.
    XmlReader reader = xslt.Transform(Server.MapPath("Source.xml"), null
                                                         new System.Xml.XmlUrlResolver());
    reader.MoveToContent();
    string resultXmlString = reader.ReadOuterXml();

    //now use that string to construct my object

    MyCustomControl mc =new MyCustomControl(resultXmlString);

    There is still the issue of doing this the Whidbey way. The XslTransform is now obsolete and replaced by the XslCompiledTransform, which, however only has overloads that return void. It is supposed to be much more efficient.

    So how do we do this in v2.0?

    TIA

    Rob

    Regards,

    Rob
  • Re: How to xsltransform to an xml string in memory?

    07-20-2005, 6:36 PM
    • Contributor
      4,280 point Contributor
    • tomasr
    • Member since 04-28-2003, 11:18 AM
    • Colombia
    • Posts 852
    Rob,

    Doing it in 2.0 is not any harder, just use any of the overloads of XslCompiledTransform.Transform() that takes an TextWriter as an argument and pass in a StringWriter there instead of the usual StreamWriter. This will get your transformed output to a StringBuilder from which you can read it back (or just use a StreamWriter on top of a MemoryStream).

    Tomas Restrepo [MVP]
    tomasr@mvps.org
  • Re: How to xsltransform to an xml string in memory?

    07-20-2005, 11:32 PM
    • Contributor
      3,639 point Contributor
    • rmprimo
    • Member since 02-24-2003, 7:25 AM
    • Posts 731
    Would you mind posting a snippet.

    TIA
    Regards,

    Rob
  • Re: How to xsltransform to an xml string in memory?

    07-21-2005, 8:24 AM
    • Contributor
      4,280 point Contributor
    • tomasr
    • Member since 04-28-2003, 11:18 AM
    • Colombia
    • Posts 852
    It should look something like this:

    XslCompiledTransform xslt = ....;

    StringBuilder buffer = new StringBuilder();
    StringWriter writer = new StringWriter(buffer);

    xslt.Transform(docToTransform, writer);

    string result = buffer.ToString();


    Tomas Restrepo [MVP]
    tomasr@mvps.org
  • Re: How to xsltransform to an xml string in memory?

    07-22-2005, 11:04 AM
    • Contributor
      3,639 point Contributor
    • rmprimo
    • Member since 02-24-2003, 7:25 AM
    • Posts 731
    Thank you
    Regards,

    Rob
Page 1 of 1 (7 items)