Click here to Skip to main content
15,897,718 members
Articles / Desktop Programming / Windows Forms

XML String Browser (just like Internet Explorer) using the WebBrowser Control

Rate me:
Please Sign up or sign in to vote.
4.48/5 (15 votes)
14 Apr 2008CPOL6 min read 125.5K   3.2K   40  
This article will show you how to view a colourful collapsible treeview for XML Strings/XmlDocument in a browser (just like Internet Explorer) without the need for XML files.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;
/*
COPYRIGHT � 2008 George Zabanah
Please feel free to use this code in your apps and distribute
but leave this header at the top.
I have one request - you may not sell this code or place on 
other websites without asking my permission first.
If you would like to contribute to improve this code, please let me know.
No warranty of any kind is implied by using this code.
Use at your own risk.
*/

namespace XmlRender
{
    // Use this control if you want a webBrowser which has been extended 
    // to allow for Xml Rendering
    public partial class XmlBrowser : WebBrowser, IDisposable
    {
        #region Types/Private Variables
        /// <summary>
        /// Type to determine which XSL/XSLT to use in transformation
        /// </summary>
        public enum XslTransformType
        {
            XSL,
            XSLT10,
            XSLT10RegExp,
            XSLT10Large
        }
        // XmlDocument to store Xml data for rendering
        private XmlDocument _xmlDocument;
        // XslDocument to use when rendering Xml
        private XslTransformType _xslTransformType;
        #endregion

        #region Constructor
        public XmlBrowser()
        {
            InitializeComponent();
        }
        #endregion

        #region Properties

        #region XmlText
        /// <summary>
        /// Property to set Xml Text for webbrowser
        /// </summary>
        [Category("XmlData")]
        [Description("Use this property to set the XmlText for rendering in the webbrowser.")]
        public string XmlText
        {
            set
            {
                if (value != string.Empty)
                {
                    _xmlDocument = new XmlDocument();
                    _xmlDocument.LoadXml(value);
                    string rtn = RenderXmlToHtml.Render(_xmlDocument,_xslTransformType,this);
                    if (!string.IsNullOrEmpty(rtn))
                        this.DocumentText = rtn;
                }
            }
            get
            {
                if (_xmlDocument == null)
                    return string.Empty;
                else
                    return _xmlDocument.OuterXml;
            }
        }
        #endregion

        #region XmlDocument
        /// <summary>
        /// Property to set XmlDocument for webbrowser
        /// </summary>
        [Category("XmlData")]
        [Description("Use this property in your code to set the System.Xml.XmlDocument for rendering in the webbrowser.")]
        public XmlDocument XmlDocument
        {
            get
            {
                return _xmlDocument;
            }
            set
            {
                if (value != null)
                {
                    _xmlDocument = value;
                    string rtn = RenderXmlToHtml.Render(_xmlDocument, _xslTransformType, this);
                    if (!string.IsNullOrEmpty(rtn))
                        this.DocumentText = rtn;
                }
            }
        }
        #endregion

        #region XmlDocumentTransformType
        /// <summary>
        /// Change this to determine which transform type to use
        /// </summary>
        [Category("XmlData")]
        [Description("Use this property to specify to use either the XSL or XSLT 1.0 compliant stylesheet for rendering.")] 
        public XslTransformType XmlDocumentTransformType
        {
            get
            {
                return _xslTransformType;
            }
            set
            {
                _xslTransformType = value;
            }
        }
        #endregion

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Australia Australia
George Zabanah has been architecting and developing solutions commercially from start to finish for over 11 years. He has been programming since he was 11 (for over 25 years now!) using many different technologies (almost all Microsoft). George is proficient in many languages and employs best practices wherever possible using design patterns, .NET, XSLT, XML, Regular Expressions, various flavours of SQL (to name just a few). His favourite tools, however, are the whiteboard and Sparx Enterprise Architect/Powerpoint. Many waking moments have been spent by George thinking about solutions in his head (even while watching tv). His best moments are the "Eureka" moments when he wakes up from a good sleep (or after watching tv) to find that his latest challenge has been solved!

Comments and Discussions