Click here to Skip to main content
15,891,607 members
Articles / Programming Languages / C#

XML Introspection and TreeView

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
26 Jan 2009CPOL2 min read 40.9K   880   26   5
Xml Introspection, TreeView node and PropertyGrid
IMG106.jpg

Introduction

I wrote this article to show how to associate data of TreeView and PropertyGrid.

Background 

I have described this on my blog but in French. (That's the reason why my English in not good.)

Using the Code 

You get data from XML (XmlDocument with XmlNodeCollection), and you use a TreeView (TreeView with TreeNode) to display them. 

When the user clicks on the TreeView, you want to display the XmlNode object in propertyGrid.

Then you have to associate the XmlNode with TreeNode, to display them for a propertyGrid, like that:  

C#
propertyGrid1.SelectedObject = yourObject; 

The first and bad solution is to use Index or Name of TreeNode to find in XmlDocument, the XmlNode used to build the TreeNode.

So, you can try, but you'll be crazy, for example associate XmlNode and TreeNode in a List, or Array... something strange like that.

My solution might not be better, but I like it.

Derive the TreeNode, to an XmlNodeTree associated TreeNode and XmlNode.

C#
public class XmlNodeTree : TreeNode
       {
           private XmlNode mNode;
           public XmlNode Node
           {
               get { return mNode; }
           }

            public XmlNodeTree(XmlNode node)
           {
               mNode = node;
               if (node.NodeType == XmlNodeType.Text)
               {
                   Text = node.InnerText;
               }
               else
               {
                   Text = node.Name;
               }

               if (node.Attributes != null)
               {
                   foreach (XmlAttribute a in node.Attributes)
                   {
                       Text += " " + a.OuterXml;
                   }
               }
           }
       }

And for filling the TreeView, it's really easy:

C#
  private void FillTreeView(TreeNodeCollection c,XmlNodeList l)
        {
            if (l == null)
            {
                return;
            }

            foreach (XmlElement e in l)
            {
                XmlNodeTree n = new XmlNodeTree(e);
                c.Add(n);
                FillTreeView(n.Nodes, e.ChildNodes);
            }
        }

Call this method by:

C#
XmlNodeTree root = new XmlNodeTree(doc.LastChild);
treeView1.Nodes.Add(root);
FillTreeView(root.Nodes,doc.LastChild.ChildNodes);

Then when the user clicks on the TreeView:

C#
XmlNodeTree currentNode = (XmlNodeTree) e.Node;
propertyGrid1.SelectedObject = currentNode.Node;

Points of Interest

So in one hundred lines of code, you have an XML viewer.  

You can modify it easily to make a real editor. I add a text widget to view the XML content.

History 

I tried to use FireBall editor to see XML in FireBall.CodeEditor with some problem, so I deliver the project to you without it.  

Please see other stuff here.

I will publish an XML introspector later to make this more complete.

Thanks to acton101 for helping to resolve a bug.

There's a problem in FillTreeView.
Replace foreach (XmlElement e in l) with foreach (XmlNode e in l).
This is because XmlText can't be cast to XmlElement.

Read the hierarchy of XmlText.

Added another test about the existence of XmlAttribute.

License

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


Written By
Software Developer (Senior) http://www.cmb-soft.com/
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 21:12
professionalManoj Kumar Choubey26-Feb-12 21:12 
Generalthanks Pin
Member 41913661-Aug-09 0:35
Member 41913661-Aug-09 0:35 
GeneralIt cannot open a valid XML file properly Pin
acton10119-Jan-09 10:54
acton10119-Jan-09 10:54 
GeneralRe: It cannot open a valid XML file properly Pin
zebulon7501826-Jan-09 13:47
zebulon7501826-Jan-09 13:47 
You right there's a problem, not during xml reading , but filling the treeview.

The exception appears in :
foreach (XmlElement e in l)
{"Unable to cast object of type 'System.Xml.XmlText' to type 'System.Xml.XmlElement'."}

There's a bug in this case :
<name>Belgian Waffles
( without attribut and text between <tag> ).

I change to make it good : XmlElement to XmlNode.

Ok, I understand, you right, thanks for reporting.
GeneralRe: It cannot open a valid XML file properly Pin
acton10127-Jan-09 5:15
acton10127-Jan-09 5:15 

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.