Click here to Skip to main content
15,867,704 members
Articles / Desktop Programming / Windows Forms

View XML Files Easily with XmlGridViewControl

Rate me:
Please Sign up or sign in to vote.
2.53/5 (6 votes)
28 Nov 2006CPOL1 min read 88.5K   3.5K   53   9
A .NET control that displays XML files as XML or in a grid control.

Sample Image - screen0.jpg

Introduction

The XmlGridViewControl is a .NET control for Windows Forms that displays an XML file in either a beautified XML format (via the WebBrowser control) or in a DataGrid. When set to display in the DataGrid, the index (zero-based) of the DataTable of the XML file/DataSet can be changed to quickly display a specific DataTable in the XML DataSet.

Using the XmlGridViewControl .NET Control

To use the XmlGridViewControl in a Windows Form or another Windows .NET control, drop an instance of the XmlGridViewControl control on the form and then just set the following properties in your form/control class:

C#
// display the XML file in a datagrid
xmlGridView.ViewMode = XmlGridView.VIEW_MODE.TABLE;
xmlGridView.DataSetTableIndex = 0;
xmlGridView.DataFilePath = "c:\\temp\SampleFile.xml";
    
//OR
    
// display the XML file in XML mode
xmlGridView.ViewMode = XmlGridView.VIEW_MODE.XML;   
xmlGridView.DataFilePath = "c:\\temp\SampleFile.xml";

Below is a demonstration of some of the different views in the XmlGridViewControl sample:

XML view of the sample XML file

XML View

Grid view of the first table in the sample XML file

Grid View Table 1

Grid view of the second table in the sample XML file

Grid View Table 2

How the XmlGridViewControl Works

The XmlGridViewControl control is basically just an instance of a DataGrid control and a WebBrowser control, and the ViewMode property determines which control is displayed (while the other is hidden). The DataFilePath property points to the XML file which is loaded and displayed, and the DataSetTableIndex determines which DataTable to display when in the TABLE mode, and the DataSet contains multiple DataTabless.

C#
public partial class XmlGridView : UserControl
{        
    private bool m_bGridViewModeReadError = false;        

    public enum VIEW_MODE {XML, TABLE}
    public VIEW_MODE ViewMode
    {
        get
        {
            return (webXmlView.Visible ? VIEW_MODE.XML : VIEW_MODE.TABLE);
        }
        set
        {
            SetViewMode(value);
        }
    }

    private string m_sDataFilePath = string.Empty;
    public string DataFilePath
    {
        get
        {
            return m_sDataFilePath;
        }
        set
        {
            m_sDataFilePath = value;
            LoadDataFile();
        }
    }

    private int m_nDataSetTableIndex = 0;
    public int DataSetTableIndex
    {
        get
        {
            return m_nDataSetTableIndex;
        }
        set
        {
            SetDataSetTableIndex(value);
        }
    }
        
    private int m_nDataTableCount = 0;
    public int DataTableCount
    {
        get { return m_nDataTableCount; }
    }

    public XmlGridView()
    {
        InitializeComponent();

        SetViewMode(VIEW_MODE.XML);
    }

    private void SetViewMode(VIEW_MODE mode)
    {
        if (m_bGridViewModeReadError == true)
        {
            mode = VIEW_MODE.XML;
        }

        if(mode == VIEW_MODE.XML)
        {
            webXmlView.Visible = true;
            grdTableView.Visible = false;
        }
        else
        {
            webXmlView.Visible = false;
            grdTableView.Visible = true;
        }
    }

    private void LoadDataFile()
    {
        m_bGridViewModeReadError = false;

        // use the webbrowser control to automatically parse the file
        webXmlView.Navigate(m_sDataFilePath);

        if ((m_sDataFilePath != string.Empty) && (File.Exists(m_sDataFilePath) == true))
        {
            // Creates a DataSet and loads it with the xml content
            try
            {
                DataSet dsXmlFile = new DataSet();
                dsXmlFile.ReadXml(m_sDataFilePath, XmlReadMode.Auto);
                m_nDataTableCount = dsXmlFile.Tables.Count; 

                grdTableView.DataSource = dsXmlFile.Tables[DataSetTableIndex]; 
            }
            catch
            {
                m_bGridViewModeReadError = true;
                m_nDataTableCount = 0;
                webXmlView.Navigate(m_sDataFilePath);
                SetViewMode(VIEW_MODE.XML);
            }
        }
        else
        {
            grdTableView.DataSource = null;
        }
    }

    private void SetDataSetTableIndex(int nTableIndex)
    {
        if (nTableIndex >= m_nDataTableCount)
            return;

        m_nDataSetTableIndex = nTableIndex;
        LoadDataFile();
    }
}

Conclusion

I hope you find this article and control useful - it's proved to be an invaluable resource when examining large or complicated XML files. Enjoy!

Updates

November 28, 2006

The control has been updated to include a DataTableCount property which represents the number of DataTables in the DataSet/XML file. The sample application has also been updated to use a ComboBox to select the current DataTable index from, instead of a TextBox.

License

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


Written By
Software Developer
United States United States
SOFTWARE: Chris Hambleton is a Software Developer with proven experience in developing both web and Windows client-server applications with WPF, ASP.NET, C#, SQL Server, VB.NET, Visual C++, and VB6.

Chris's website is at ChrisHambleton.com and he has a small web-hosting/consulting business called CustomersInFocus.com. He has several other websites such as EzekielWatch.com, iWriterPro.com, and BookBlitzer.com.

WRITING: He has also written several fiction books ("The Time of Jacob's Trouble" and "Endeavor in Time"), available at CWHambleton.com and of course, at Amazon.com (Full Amazon Profile).

Comments and Discussions

 
QuestionIs it possible to pass two or three parameters? Pin
Member 1131125017-Dec-14 1:40
Member 1131125017-Dec-14 1:40 
Generalexactly what I needed but didn't work Pin
rj4529-Apr-11 8:58
rj4529-Apr-11 8:58 
GeneralxmlControl Pin
vp200015-Feb-07 15:41
vp200015-Feb-07 15:41 
Questioncan someon econvert the demo project into .NET Pin
Member 33817526-Feb-07 17:54
Member 33817526-Feb-07 17:54 
QuestionXmlObject instead of File Path. Pin
Matronex27-Dec-06 6:17
Matronex27-Dec-06 6:17 
AnswerRe: XmlObject instead of File Path. Pin
Chris Hambleton28-Dec-06 17:54
Chris Hambleton28-Dec-06 17:54 
All you really need to do is call something like: xmldoc.Save(sSomeTempFilePath), and then pass the sSomeTempFilePath into the WebBrowser.Navigate(). That's really all thats happening to display the XmlDocument in the Viewer/Browser...
QuestionDoes this work with anything except your demo data? Pin
Joe Sonderegger27-Nov-06 8:15
Joe Sonderegger27-Nov-06 8:15 
AnswerRe: Does this work with anything except your demo data? Pin
KJJ228-Nov-06 3:56
KJJ228-Nov-06 3:56 
AnswerRe: Does this work with anything except your demo data? Pin
Chris Hambleton28-Nov-06 6:09
Chris Hambleton28-Nov-06 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.