iNerd Blog

Xml-to-Html with XSLT in ASP.NET MVC

Posted in Programming by Ari on 18 May 2010

This is how you write an XML-to-HTML custom actionresult using XSLT , in ASP.NET MVC. Code is not originally mine. Just copy everything and it should work.
So create a helper class and name it XmlActionresult and follow exactly as bellow.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Xml.XPath;

namespace MvcApplication1.Controllers
{
    public class XmlActionResult :ActionResult
    {
     #region Fields
        private XmlDocument _xmlDocument;
        private XPathNavigator _xpathNavigator;
        private string _documentContent;
        private string _documentSource;
        private XslCompiledTransform _transform;
        private static XslCompiledTransform _transparentTransform;
        private string _transformSource;
        private XsltArgumentList _transformArgumentList;
        #endregion

        #region Properties
        /// 
        /// Gets or sets the XmlDocument to write to the response.
        /// 
        public XmlDocument Document
        {
            get { return _xmlDocument; }
            set
            {
                _xmlDocument = value;
                _documentContent = String.Empty;
                _documentSource = String.Empty;
                _xpathNavigator = null;
            }
        }

        /// 
        /// Gets or sets a string that contains the XML document to write to the response.
        /// 
        public string DocumentContent
        {
            get { return _documentContent; }
            set
            {
                _xmlDocument = null;
                _documentContent = value;
                _documentSource = String.Empty;
                _xpathNavigator = null;
            }
        }

        /// 
        /// Gets or sets the path to an XML document to write to the response.
        /// 
        public string DocumentSource
        {
            get { return _documentSource; }
            set
            {
                _xmlDocument = null;
                _documentContent = String.Empty;
                _documentSource = value;
                _xpathNavigator = null;
            }
        }

        /// 
        /// Gets or sets the XslCompiledTransform object that formats the XML document before it is written to the response.
        /// 
        public XslCompiledTransform Transform
        {
            get { return _transform; }
            set
            {
                _transform = value;
                _transformSource = String.Empty;
            }
        }

        /// 
        /// Gets or sets a XsltArgumentList that contains a list of optional arguments passed to the style sheet and used during the XSL transformation.
        /// 
        public XsltArgumentList TransformArgumentList
        {
            get { return _transformArgumentList; }
            set { _transformArgumentList = value; }
        }

        /// 
        /// Gets or sets the path to an XSL Transformation style sheet that formats the XML document before it is written to the response.
        /// 
        public string TransformSource
        {
            get { return _transformSource; }
            set
            {
                _transform = null;
                _transformSource = value;
            }
        }

        /// 
        /// Gets or sets a cursor model for navigating and editing the XML data to write to the response.
        /// 
        public XPathNavigator XPathNavigator
        {
            get { return _xpathNavigator; }
            set
            {
                _xmlDocument = null;
                _documentContent = String.Empty;
                _documentSource = String.Empty;
                _xpathNavigator = value;
            }
        }
        #endregion

        #region Constructor
        static XmlActionResult()
        {
            XmlTextReader copyTransformReader = new XmlTextReader(new StringReader("   "));
            _transparentTransform = new XslCompiledTransform();
            _transparentTransform.Load(copyTransformReader);
        }
        #endregion

        #region Methods
        public override void ExecuteResult(ControllerContext context)
        {
            XPathDocument xpathDocument = null;
            //Checking if we have been given XmlDocument or XPathNavigator directly
            if ((_xmlDocument == null) && (_xpathNavigator == null))
            {
                //Checking if we have document content
                if (!String.IsNullOrEmpty(_documentContent))
                {
                    StringReader documentReader = new StringReader(_documentContent);
                    xpathDocument = new XPathDocument(documentReader);
                }
                //Checking if we have path for document
                else if (!String.IsNullOrEmpty(_documentSource) && (_documentSource.Trim().Length != 0))
                {
                    //Checking if path is absolute or relative
                    if (!Path.IsPathRooted(_documentSource))
                        //Mapping the relative path
                        _documentSource = context.HttpContext.Server.MapPath(_documentSource);

                    //Loading XML from file into XPathDocument
                    using (FileStream documentStream = new FileStream(_documentSource, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        XmlTextReader documentReader = new XmlTextReader(documentStream);
                        xpathDocument = new XPathDocument(documentReader);
                    }
                }
            }

            //Checking if we have been given XslCompiledTransform directly, or do we have path for transform
            if ((_transform == null) && (!String.IsNullOrEmpty(_transformSource) && (_transformSource.Trim().Length != 0)))
            {
                //Checking if path is absolute or relative
                if (!Path.IsPathRooted(_transformSource))
                    //Mapping the relative path
                    _transformSource = context.HttpContext.Server.MapPath(_transformSource);

                //Loading XSLT from file into XslCompiledTransform
                using (FileStream transformStream = new FileStream(_transformSource, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    XmlTextReader tranformReader = new XmlTextReader(transformStream);
                    _transform = new XslCompiledTransform();
                    _transform.Load(tranformReader);
                }
            }

            //Checking if we have XML in any form
            if (((_xmlDocument != null) || (xpathDocument != null)) || (_xpathNavigator != null))
            {
                context.HttpContext.Response.ContentType = "text/html";

                //Checking if we have XSLT
                if (_transform == null)
                    //If not, let's use transparent one
                    _transform = _transparentTransform;

                //Perform transformation based on form in which we have our XML
                if (_xmlDocument != null)
                    _transform.Transform((IXPathNavigable)_xmlDocument, _transformArgumentList, context.HttpContext.Response.Output);
                else if (_xpathNavigator != null)
                    _transform.Transform(_xpathNavigator, _transformArgumentList, context.HttpContext.Response.Output);
                else
                    _transform.Transform((IXPathNavigable)xpathDocument, _transformArgumentList, context.HttpContext.Response.Output);
            }
        }
        #endregion
    }
}


Next on your controller:

 public XmlActionResult Test()
        {
            XmlDocument cdCatalogDocument = new XmlDocument();
            cdCatalogDocument.Load(Server.MapPath("~/App_Data/cdcatalog.xml"));

            XmlActionResult result = new XmlActionResult();
            result.Document = cdCatalogDocument;
            result.TransformSource = Server.MapPath("~/App_Data/cdcatalog.xsl");
            return result;
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: