Dan Rigsby - Coding Up Style

.Net, C#, & Wcf Development

XsltTransformer: Transforming an xml string with an xslt string

Posted by Dan Rigsby on December 21st, 2007

There are many times when I have xml and xslt as strings and need the transformed data as a string.  I put together a utility class to take care of this.

/// <summary>
/// Transforms the specified xml string using the specified xslt string.
/// </summary>
/// <param name="xml">The xml string to transform.</param>
/// <param name="xslt">The xslt string to transform with.</param>
/// <returns>The transformed xml string.</returns>
public static string Transform(
    string xml,
    string xslt
    )
{
    string output = String.Empty;
    StringWriter stringWriter = null;
    XmlTextWriter xmlWriter = null;
    XmlDocument inputDocument = new XmlDocument();
    XmlDocument xsltDocument = new XmlDocument();
    System.Xml.Xsl.XslCompiledTransform xslCompiledTransformer = null;

    try
    {
        // Create Xml Document
        inputDocument.LoadXml(xml);

        // Create XsltDocument
        xsltDocument.LoadXml(xslt);

        stringWriter = new StringWriter();
        xmlWriter = new XmlTextWriter(stringWriter);

        // Apply the generated text stylesheet. 
    #if DEBUG
        xslCompiledTransformer =
            new System.Xml.Xsl.XslCompiledTransform(true);
    #else
        xslCompiledTransformer =
            new System.Xml.Xsl.XslCompiledTransform(false);
    #endif
        xslCompiledTransformer.Load(xsltDocument);
        xslCompiledTransformer.Transform(inputDocument, xmlWriter);

        // Generate the output Xml
        output = stringWriter.GetStringBuilder().ToString();
    }
    finally
    {
        if (xmlWriter != null)
        {
            xmlWriter.Close();
        }

        if (stringWriter != null)
        {
            stringWriter.Dispose();
        }

        if (xslCompiledTransformer != null)
        {
            xslCompiledTransformer.TemporaryFiles.Delete();
        }
    }

    return output;
}

 

A couple of things of note:

  1. When I first started using this, I had this running in memory and it would transform over 100 times an hour.  We had a variety of stylesheets, and the xml was almost always different.  If we didn’t make a call to TemporaryFiles.Delete(), then these temporary files would be kept and overtime this resulted in a lot of useless files.  This seems like a bug to me that this files aren’t cleaned up by default, but I am sure there is some clever reason for this.  It may depend on how you are implementing it.  I do like having the flexibility of deciding whether or not to keep them though.  You can read more about these options on the MSDN page for XsltCompiledTransform.
  2. I had thought of making the XsltCompiledTransform a static variable, but depending on how this method is used, I didn’t want to always have this loaded in memory.  If you make use of this code, you may decide to do this.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>