Click here to Skip to main content
15,893,381 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.1K   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.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
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 XMLRenderTestApp
{
    public partial class Form1 : Form
    {
        // used to store location of xml files
        string _xmlDirectory;

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            // Set up xmlDirectory and load combo box.  
            //
            // when you build the project all files are copied to $(TargetDir)\XML Files
            // just add any additional files you want to the XML files directory of this project
            FileInfo fi = new FileInfo(Assembly.GetExecutingAssembly().FullName);
            _xmlDirectory = fi.DirectoryName + @"\XML Files\";
            foreach(FileInfo file in new DirectoryInfo(_xmlDirectory).GetFiles("*.*"))
            {
                comboBox1.Items.Add(file.Name);
            }
        }
        #endregion

        #region comboBox select
        /// <summary>
        /// Xml file to load into a string for rendering.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XmlDocument _xd;
            try
            {
                _xd = new XmlDocument();
                string fileName = _xmlDirectory + comboBox1.SelectedItem.ToString();
                System.Uri _uri = new Uri(fileName);

                _xd.Load(fileName);

                // the following lines are of particular interest.  
                // I'm rendering an XML string to HTML (using good old XSL).
                // This gives us the flexibility to build XML and 
                // present it in the browser in a nice way.
                //
                // I have to do this because we can't send an XML string to the browser 
                // and expect it to render like IE does

                // Send an XmlDocument to the XmlBrowser
                xmlBrowser1.XmlDocument = _xd;

                // Uncomment the following to send Xml Text to the XmlBrowser
                //xmlBrowser1.XmlText = _xd.OuterXml;

                // Uncomment the following to see how the webbrowser control renders XML text
                //xmlBrowser1.DocumentText = _xd.OuterXml;

                // Uncomment the following to compare to WebBrowser native rendering of Xml Documents
                //xmlBrowser1.Url = _uri;
            }
            finally
            {
                _xd = null;
            }
        }
        #endregion

        #region RadioButtons
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            xmlBrowser1.XmlDocumentTransformType = XmlRender.XmlBrowser.XslTransformType.XSL;
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            xmlBrowser1.XmlDocumentTransformType = XmlRender.XmlBrowser.XslTransformType.XSLT10;
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            xmlBrowser1.XmlDocumentTransformType = XmlRender.XmlBrowser.XslTransformType.XSLT10RegExp;
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            xmlBrowser1.XmlDocumentTransformType = XmlRender.XmlBrowser.XslTransformType.XSLT10Basic;
        }
        #endregion

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            xmlBrowser1.XmlDocumentTransformType = XmlRender.XmlBrowser.XslTransformType.XSLT10Cdata;

        }

    }
}

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