XSLT question, how to transform xml when I have xslt file stored, but object in mem?

Posted by JL on Stack Overflow See other posts from Stack Overflow or by JL
Published on 2010-03-30T20:50:02Z Indexed on 2010/03/30 20:53 UTC
Read the original article Hit count: 285

Filed under:

I have a function that takes 2 parameters : 1 = XML file, 2 = XSLT file, then performs a transformation and returns the resulting HTML.

Here is the function:

/// <summary>
/// Will apply an XSLT style to any XML file and return the rendered HTML.
/// </summary>
/// <param name="xmlFileName">
/// The file name of the XML document.
/// </param>
/// <param name="xslFileName">
/// The file name of the XSL document.
/// </param>
/// <returns>
/// The rendered HTML.
/// </returns>
public string TransformXml(string xmlFileName, string xslFileName)
{
    var xtr = new XmlTextReader(xmlFileName)
                  {
                      WhitespaceHandling = WhitespaceHandling.None
                  };
    var xd = new XmlDocument();
    xd.Load(xtr);

    var xslt = new System.Xml.Xsl.XslCompiledTransform();
    xslt.Load(xslFileName);
    var stm = new MemoryStream();
    xslt.Transform(xd, null, stm);
    stm.Position = 1;
    var sr = new StreamReader(stm);
    xtr.Close();
    return sr.ReadToEnd();
}

I want to change the function not to accept a file name, but rather a strongly typed object de-serialized (now in the form of a variable). Is this possible?

So keep the xslt coming from a file, but the xml input should be a the serialized xml of the object I pass, and I want to do this without file system IO.

© Stack Overflow or respective owner

Related posts about Xml