Click here to Skip to main content
6,935,947 members and growing! (18,150 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

View XML Files Easily with XmlGridViewControl

By Chris Hambleton

.NET control that displays XML files as XML or in a grid control
C#2.0, Windows, .NET2.0, WinForms, VS2005, Dev
Posted:22 Nov 2006
Updated:28 Nov 2006
Views:39,751
Bookmarked:44 times
Unedited contribution
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 1.71 Rating: 2.44 out of 5
2 votes, 40.0%
1

2
1 vote, 20.0%
3
2 votes, 40.0%
4

5

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 Web Browser control) or in a datagrid view. When set to display in the datagrid, the index (zero-based) of the DataTable 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:

    // 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 1st Table in the Sample XML File

Grid View Table 1

Grid View of the 2nd 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 web-browser 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 DataTables.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chris Hambleton


Member
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).
Occupation: Software Developer
Location: United States United States

Other popular Miscellaneous articles:

 
Article Top
 
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralxmlControl Pinmembervp200016:41 15 Feb '07  
Generalcan someon econvert the demo project into .NET Pinmember18:54 6 Feb '07  
QuestionXmlObject instead of File Path. PinmemberMaurice_837:17 27 Dec '06  
AnswerRe: XmlObject instead of File Path. PinmemberChris Hambleton18:54 28 Dec '06  
QuestionDoes this work with anything except your demo data? PinmemberNice Life9:15 27 Nov '06  
AnswerRe: Does this work with anything except your demo data? PinmemberKJJ24:56 28 Nov '06  
AnswerRe: Does this work with anything except your demo data? PinmemberChris Hambleton7:09 28 Nov '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 28 Nov 2006
Editor:
Copyright 2006 by Chris Hambleton
Everything else Copyright © CodeProject, 1999-2010
Web09 | Advertise on the Code Project