Perform an XSL Transformation using XML and XSL content and with some XSLT arguments : XML Transform « XML « C# / C Sharp






Perform an XSL Transformation using XML and XSL content and with some XSLT arguments

   
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace SharePointOfView.Utilities
{
    /// <summary>
    /// SharePointOfView XSLT Utility class
    /// </summary>
    public static class XSLT
    {
        /// <summary>
        /// Perform an XSL Transformation using XML and XSL content
        /// </summary>
        /// <param name="xmlContent">XML source</param>
        /// <param name="xsltContent">XSL source</param>
        /// <returns>HTML</returns>
        public static string XSLTransform(string xmlContent, string xsltContent)
        {
            return XSLTransform(xmlContent, xsltContent, null);
        }

        /// <summary>
        /// Perform an XSL Transformation using XML and XSL content and with some XSLT arguments
        /// </summary>
        /// <param name="xmlContent">XML source</param>
        /// <param name="xsltContent">XSL source</param>
        /// <param name="xsltArgs">XSLT argument list</param>
        /// <returns>HTML</returns>
        public static string XSLTransform(string xmlContent, string xsltContent, XsltArgumentList xsltArgs)
        {
            string s;
            try
            {
                // Initialize a memorystring for output
                MemoryStream mem = new MemoryStream();
                // Initialize a StreamReader using the MemoryStream
                StreamReader sr = new StreamReader(mem, UTF8Encoding.UTF8);
                // Initialize an XSLTransform object
                XslTransform xTrans = new XslTransform();
                // Load XML Content
                XmlTextReader xmlReader = new XmlTextReader(xmlContent, XmlNodeType.Document, null);
                XPathDocument xmlDoc = new XPathDocument(xmlReader);
                // Load XSL Content
                XmlTextReader xslReader = new XmlTextReader(xsltContent, XmlNodeType.Document, null);
                XPathDocument xslDoc = new XPathDocument(xslReader);
                // Load XSL into transform
                xTrans.Load(xslDoc.CreateNavigator());
                // Perform transformation
                xTrans.Transform(xmlDoc.CreateNavigator(), xsltArgs, mem);
                // Copy stream to string
                mem.Position = 0;
                s = sr.ReadToEnd();
                sr.Close();
                mem.Close();
            }
            catch (Exception ex)
            {
                s = ex.Message;
            }
            return s;
        }
    }
}

   
    
    
  








Related examples in the same category

1.Use XslCompiledTransform
2.Perform an XSL Transform
3.Read command line input and do the xml xsl translation
4.Illustrates the XslTransform classIllustrates the XslTransform class
5.XML transformation: transform XML file to HTML file
6.Xslt Transformation Helper