Posted on May 7, 2013 by

Quick C# XML Former

While I was working at FedEx, we had a lot of systems that would need to communicate with each other. These systems often communicated using an advanced messaging queue that was simply called the broker. The broker logged everything it did in great detail, the only issue was that it logged everything in a single line of XML that ended up being extremely long (which means it had no line breaks and made it hard to find the info you were looking for).

I wrote this quick tool that will take any XML and show it in a tree-view. This is a great plugin that many text editors have now that makes this easier, but just in case you want something to mess around with: here ya go.

The code behind is quite simple. The bread and butter fall into two methods mainly:

FillTree is the first method which is the parent method responsible for the flow of adding elements to the tree.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        private void FillTree(TreeView currenttree, TabPage tabName)
        {
            try
            {
                XmlDocument dom = new XmlDocument();
                dom.Load(Application.StartupPath + "\\xmldump.xml");
                currenttree.Nodes.Clear();
                if (dom.DocumentElement.Name.Contains(':'))
                {
                    currenttree.Nodes.Add(new TreeNode(dom.DocumentElement.Name.Split(':')[1]));
                }
                else
                {
                    currenttree.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
                }
                TreeNode tNode = new TreeNode();
                tNode = currenttree.Nodes[0];
                currenttree.Nodes[0].ContextMenuStrip = treeviewmenu;
                AddNode(dom.DocumentElement, tNode);
                if (chkClearInput.Checked == true)
                {
                    txtInputXML.Clear();
                }
                treecount += 1;
                if (booExpandTree == true)
                {
                    currenttree.ExpandAll();
                }
                booSuccess = true;
            }
            catch (XmlException xmlex)
            {
                booSuccess = false;
                MessageBox.Show("XML Malformed. Reason for Error: - " + xmlex.Message, "Error");
            }
            catch (Exception ex)
            {
                booSuccess = false;
                MessageBox.Show("Non XML Error Occured: " + ex.Message, "Error");
            }
        }

AddNode is the second. It is simply responsible for taking the XML node and filling the tree branch with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
        {
            string strCheck;
            XmlNode xNode;
            TreeNode tNode;
            XmlNodeList nodeList;
            int intLength;
            int intStart;
            int intEnd;
            int i = 0;
            if (inXmlNode.HasChildNodes)
            {
                nodeList = inXmlNode.ChildNodes;
                for (i = 0; i <= nodeList.Count - 1; i++) { string tester; xNode = inXmlNode.ChildNodes[i]; tester = xNode.Name.ToLower(); if (xNode.Name.Contains(':')) { inTreeNode.Nodes.Add(new TreeNode(xNode.Name.Split(':')[1])); } else { inTreeNode.Nodes.Add(new TreeNode(xNode.Name)); } tNode = inTreeNode.Nodes[(int)i]; tNode.ContextMenuStrip = treeviewmenu; AddNode(xNode, tNode); } } else { if ((inXmlNode.OuterXml).Trim().Contains(">

I added it to a WinForm and added some exporting functionality. You can see it below, I took my RSS feed from here, and put it into the tree view:

Simple XML Former UI

You can download XML Former for you to try for yourself. Let me know if you have any questions.