XSLT question, how to transform xml when I have xslt file stored, but object in mem?
- by JL
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.