Recursion, parsing xml file with attributes into treeview c#

Solution 1:

You need to move the loop through attributes out of the loop through child nodes:

    private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {
        // Loop through the XML nodes until the leaf is reached.
        // Add the nodes to the TreeView during the looping process.

        if (inXmlNode.HasChildNodes)
        {
            //Check if the XmlNode has attributes
            foreach (XmlAttribute att in inXmlNode.Attributes)
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
            }

            var nodeList = inXmlNode.ChildNodes;
            for (int i = 0; i < nodeList.Count; i++)
            {
                var xNode = inXmlNode.ChildNodes[i];
                var tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(xNode.Name))];
                AddNode(xNode, tNode);
            }
        }
        else
        {
            // Here you need to pull the data from the XmlNode based on the
            // type of node, whether attribute values are required, and so forth.
            inTreeNode.Text = (inXmlNode.OuterXml).Trim();
        }
        treeView1.ExpandAll();
    }

Update

If you want to filter out namespace attributes, you can add extension methods:

public static class XmlNodeExtensions
{
    public static bool IsDefaultNamespaceDeclaration(this XmlAttribute attr)
    {
        if (attr == null)
            return false;
        if (attr.NamespaceURI != "http://www.w3.org/2000/xmlns/")
            return false;
        return attr.Name == "xmlns";
    }

    public static bool IsNamespaceDeclaration(this XmlAttribute attr)
    {
        if (attr == null)
            return false;
        if (attr.NamespaceURI != "http://www.w3.org/2000/xmlns/")
            return false;
        return attr.Name == "xmlns" || attr.Name.StartsWith("xmlns:");
    }
}

Then use it to skip unwanted XmlAttribute instances. You can also explicitly set the text of all nodes of type XmlElement to be name + attribute data, not just those elements with children, using OuterXml only for text nodes:

    private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {
        if (inXmlNode is XmlElement)
        {
            // An element.  Display element name + attribute names & values.
            foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsNamespaceDeclaration()))
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
            }
            // Add children
            foreach (XmlNode xNode in inXmlNode.ChildNodes)
            {
                var tNode = inTreeNode.Nodes[inTreeNode.Nodes.Add(new TreeNode(xNode.Name))];
                AddNode(xNode, tNode);
            }
        }
        else
        {
            // Not an element.  Character data, comment, etc.  Display all text.
            inTreeNode.Text = (inXmlNode.OuterXml).Trim();
        }
        treeView1.ExpandAll();
    }

If you really want to filter out just the default namespace definitions but leave others, you could do:

            // An element.  Display element name + attribute names & values.
            foreach (var att in inXmlNode.Attributes.Cast<XmlAttribute>().Where(a => !a.IsDefaultNamespaceDeclaration()))
            {
                inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
            }

Incidentally, I don't recommend doing this since the following actually mean the same thing, namely an element with local name DataConfiguration in the namespace http://somenamespace:

<ss:DataConfiguration xmlns:ss="http://somenamespace"/>
<DataConfiguration xmlns="http://somenamespace"/>

Your tree will display the namespace for the first element but not the second.

Update 2

To include the XmlDeclaration in the tree, change the top level loop to be:

        treeView1.Nodes.Clear();
        foreach (XmlNode xNode in dom.ChildNodes)
        {
            var tNode = treeView1.Nodes[treeView1.Nodes.Add(new TreeNode(xNode.Name))];
            AddNode(xNode, tNode);
        }

Update 3

Put the loop to include the XmlDeclaration loop in DisplayTreeView:

    private void DisplayTreeView(string pathname)
    {
        try
        {
            // SECTION 1. Create a DOM Document and load the XML data into it.
            XmlDocument dom = new XmlDocument();
            dom.Load(pathname);

            // SECTION 2. Initialize the TreeView control.
            treeView1.Nodes.Clear();

            // SECTION 3. Populate the TreeView with the XML nodes.
            foreach (XmlNode xNode in dom.ChildNodes)
            {
                var tNode = treeView1.Nodes[treeView1.Nodes.Add(new TreeNode(xNode.Name))];
                AddNode(xNode, tNode);
            }
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }