iNerd Blog

Render XML-to-HTML to be included in ASP.NET MVC masterpage

Posted in Programming by Ari on 18 May 2010

Again this is not my code, original post can be found here. So in the previous post, i managed to transform from xml to html in asp.net mvc, though with that method the resulting page opens up in a new window which is not quite what i expected. With this RenderXml extension method, result page will be embedded into the master page and it looks so much better.

To do that create a static class RenderXmlExtensions, copy this code below:


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

namespace MvcApplication1.Controllers
{
    /// 
    /// Provides support for rendering a XML.
    /// 
    public static class RenderXmlExtensions
    {
        #region Fields
        private static XslCompiledTransform _transparentTransform;
        #endregion

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

        #region Methods
        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML document
        ///
The XSLT transformation
        public static void RenderXml(this HtmlHelper htmlHelper, XmlDocument xmlDocument, XslCompiledTransform transform)
        {
            RenderXmlInternal(htmlHelper, xmlDocument, null, String.Empty, transform, String.Empty, null);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML document
        ///
The XSLT transformation
        ///
The XSLT transformation arguments list
        public static void RenderXml(this HtmlHelper htmlHelper, XmlDocument xmlDocument, XslCompiledTransform transform, XsltArgumentList transformArgumentList)
        {
            RenderXmlInternal(htmlHelper, xmlDocument, null, String.Empty, transform, String.Empty, transformArgumentList);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML document
        ///
The XSLT transformation source
        public static void RenderXml(this HtmlHelper htmlHelper, XmlDocument xmlDocument, string transformSource)
        {
            RenderXmlInternal(htmlHelper, xmlDocument, null, String.Empty, null, transformSource, null);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML document
        ///
The XSLT transformation source
        ///
The XSLT transformation arguments list
        public static void RenderXml(this HtmlHelper htmlHelper, XmlDocument xmlDocument, string transformSource, XsltArgumentList transformArgumentList)
        {
            RenderXmlInternal(htmlHelper, xmlDocument, null, String.Empty, null, transformSource, transformArgumentList);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML XPath navigator
        ///
The XSLT transformation
        public static void RenderXml(this HtmlHelper htmlHelper, XPathNavigator xpathNavigator, XslCompiledTransform transform)
        {
            RenderXmlInternal(htmlHelper, null, xpathNavigator, String.Empty, transform, String.Empty, null);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML XPath navigator
        ///
The XSLT transformation
        ///
The XSLT transformation arguments list
        public static void RenderXml(this HtmlHelper htmlHelper, XPathNavigator xpathNavigator, XslCompiledTransform transform, XsltArgumentList transformArgumentList)
        {
            RenderXmlInternal(htmlHelper, null, xpathNavigator, String.Empty, transform, String.Empty, transformArgumentList);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML XPath navigator
        ///
The XSLT transformation source
        public static void RenderXml(this HtmlHelper htmlHelper, XPathNavigator xpathNavigator, string transformSource)
        {
            RenderXmlInternal(htmlHelper, null, xpathNavigator, String.Empty, null, transformSource, null);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML XPath navigator
        ///
The XSLT transformation source
        ///
The XSLT transformation arguments list
        public static void RenderXml(this HtmlHelper htmlHelper, XPathNavigator xpathNavigator, string transformSource, XsltArgumentList transformArgumentList)
        {
            RenderXmlInternal(htmlHelper, null, xpathNavigator, String.Empty, null, transformSource, transformArgumentList);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML path
        ///
The XSLT transformation
        public static void RenderXml(this HtmlHelper htmlHelper, string documentSource, XslCompiledTransform transform)
        {
            RenderXmlInternal(htmlHelper, null, null, documentSource, transform, String.Empty, null);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML path
        ///
The XSLT transformation
        ///
The XSLT transformation arguments list
        public static void RenderXml(this HtmlHelper htmlHelper, string documentSource, XslCompiledTransform transform, XsltArgumentList transformArgumentList)
        {
            RenderXmlInternal(htmlHelper, null, null, documentSource, transform, String.Empty, transformArgumentList);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML path
        ///
The XSLT transformation source
        public static void RenderXml(this HtmlHelper htmlHelper, string documentSource, string transformSource)
        {
            RenderXmlInternal(htmlHelper, null, null, documentSource, null, transformSource, null);
        }

        /// 
        /// Renders the specified XML by using the specified HMTL helper.
        /// 
        ///
The HTML helper
        ///
The XML path
        ///
The XSLT transformation source
        ///
The XSLT transformation arguments list
        public static void RenderXml(this HtmlHelper htmlHelper, string documentSource, string transformSource, XsltArgumentList transformArgumentList)
        {
            RenderXmlInternal(htmlHelper, null, null, documentSource, null, transformSource, transformArgumentList);
        }

        private static void RenderXmlInternal(HtmlHelper htmlHelper, XmlDocument xmlDocument, XPathNavigator xpathNavigator, string documentSource, XslCompiledTransform transform, string transformSource, XsltArgumentList transformArgumentList)
        {
            XPathDocument xpathDocument = null;
            //Checking if we have been given XmlDocument or XPathNavigator directly, or do we have path for document
            if ((xmlDocument == null) && (xpathNavigator == null) && (!String.IsNullOrEmpty(documentSource) && (documentSource.Trim().Length != 0)))
            {
                //Checking if path is absolute or relative
                if (!Path.IsPathRooted(documentSource))
                    //Mapping the relative path
                    documentSource = htmlHelper.ViewContext.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 = htmlHelper.ViewContext.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))
            {
                //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, htmlHelper.ViewContext.HttpContext.Response.Output);
                else if (xpathNavigator != null)
                    transform.Transform(xpathNavigator, transformArgumentList, htmlHelper.ViewContext.HttpContext.Response.Output);
                else
                    transform.Transform((IXPathNavigable)xpathDocument, transformArgumentList, htmlHelper.ViewContext.HttpContext.Response.Output);
            }
        }
        #endregion
    }
}

In the controller prepare an action method, which will pass our XML to view

   public ActionResult RenderXml()
        {
            XmlDocument cdCatalogDocument = new XmlDocument();
            cdCatalogDocument.Load(Server.MapPath("~/App_Data/cdcatalog.xml"));
            return View(cdCatalogDocument);
        }

And finally call the RenderXml extension method in the contentholder

   <%  Html.RenderXml((System.Xml.XmlDocument)Model, "~/App_Data/cdcatalog.xsl"); %>
Advertisements

One Response

Subscribe to comments with RSS.

  1. […] to VoteRender XML-to-HTML to be included in ASP.NET MVC masterpage (5/17/2010)Monday, May 17, 2010 from AriAgain this is not my code, original post can be found here. So in the […]


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: