0

I have written a code that reads an XMl file and then construct a tree view of XML file using the nodes name. I would like to know how could I have attributes instead of the components(nodes) name?

For example, in the following XML file instead of action(s) I would like to print in the tree view copy or paste, etc, except for the first two parent nodes (Report and test).

XML file:

<Report version="2.1" xmlns="http://www.froglogic.com/XML2">
<test name="Example">
    <action name="delet">
        this is delet
    </action>
    <action name="copy">
        this is copy
    </action>
    <action name="paste">
        this is paste
    </action>
    <action name="manipulate">
        this is manipulate
    </action>
    <action name="copy">
        this is copy
    </action>
    <action name="paste">
        this is paste
    </action>
    <action name="manipulate">
        this is manipulate
    </action>
    <action name="change">
        this is change
    </action>
</test>
</Report>

and the C# code:

private void File2_load(object sender, EventArgs e)
    {
        try
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(File2Path.Text);

            treeView2.Nodes.Clear();
            treeView2.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
            TreeNode tNode = new TreeNode();
            tNode = treeView2.Nodes[0];

            AddNode(doc.DocumentElement, tNode);
            treeView2.ExpandAll();
            treeView2.CheckBoxes = true;
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Update

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {
        XmlNode xNode;
        TreeNode tNode;
        XmlNodeList nodeList;
        int i;

        if (inXmlNode.HasChildNodes)
        {
            nodeList = inXmlNode.ChildNodes;
            for (i = 0; i <= nodeList.Count - 1; i++)
            {
                xNode = inXmlNode.ChildNodes[i];
                inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                tNode = inTreeNode.Nodes[i];
                AddNode(xNode, tNode);
            }
        }
        else
        {
            inTreeNode.Text = (inXmlNode.OuterXml).Trim();
        }
    }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Possible duplicate of [Getting attribute value of an XML Document using C#](http://stackoverflow.com/questions/3750678/getting-attribute-value-of-an-xml-document-using-c-sharp) – har07 Nov 24 '15 at 12:40
  • But What I need to have is the first two parrents name and then for the rest shows in the tree view attributes. –  Nov 24 '15 at 12:45
  • Show us the code of the method `AddNode` you call and we can help changing it. – Martin Honnen Nov 24 '15 at 12:48
  • @MartinHonnen please see the update. –  Nov 24 '15 at 13:20
  • You can not have two attributes with the same name manipulate. – jdweng Nov 24 '15 at 13:39

1 Answers1

0

Change

            xNode = inXmlNode.ChildNodes[i];
            inTreeNode.Nodes.Add(new TreeNode(xNode.Name));

to

            xNode = inXmlNode.ChildNodes[i];
            TreeNode childTreeNode;
            if (xNode.LocalName == "action" && xNode.NodeType == XmlNodeType.Element)
            {
              childTreeNode = new TreeNode(xNode.Attributes["name"].Value);
            }
            else
            {
              childTreeNode = new TreeNode(xNode.Name);
            }

            inTreeNode.Nodes.Add(childTreeNode);

You might want to add further checks on NodeType and LocalName, depending on the complexity of your XML input and the possible nodes that can be contained in the XML input.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • I have changed `Getattributes("name")` to `Attributes["name"].Value`. Becaue there was an error. –  Nov 25 '15 at 07:27
  • @Saber, yes, sorry about that, `GetAttribute` is a method on `XmlElement` so I would have needed to cast first to an element. I have edited the sample, if your problem is now solved then you can accept the answer. – Martin Honnen Nov 25 '15 at 09:45