How to show all attributes in xml?

fzkim 61 Reputation points
2022-05-24T09:16:08.84+00:00

I want to show all attributes in treeview with my code.
How can I develop this code? Thank you!

XDocument document;
public void button1_Click(object sender, EventArgs e)
{
    document = XDocument.Load("test.xml");
    AddNodes(document.Root, treeView1.Nodes);

    treeView1.ExpandAll();
}

public void AddNodes(XElement element, TreeNodeCollection parent)
{
    var count = element.Elements().Count();

    if (count == 0)
    {
        var node = parent.Add(element.Value);
        node.Tag = element;
    }
    else
    {
        var node = parent.Add(element.Name.ToString());
        node.Tag = element;

        foreach (var child in element.Elements())
        {
            AddNodes(child, node.Nodes);
        }
    }
}
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,862 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,452 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,316 Reputation points Microsoft Vendor
    2022-05-25T02:23:59.997+00:00

    @fzkim , Welcome to Microsoft Q&A, you could try the following code to show all attributes in treeview.

    Code:

      public void AddNodes(XElement element, TreeNodeCollection parent)  
        {  
            var count = element.Elements().Count();  
            if (count == 0)  
            {  
                //var node = parent.Add(element.Name.ToString());  
                //node.Tag = element;  
                string attbriutename = "";  
                if (element.HasAttributes)  
                {  
                  
                    attbriutename = element.Name.ToString();  
                    foreach (XAttribute att in element.Attributes())  
                    {  
                        attbriutename += " " + att.ToString();  
                    }  
                }  
                else  
                {  
                    attbriutename = element.Name.ToString();  
                }  
                var node = parent.Add(attbriutename);  
                node.Nodes.Add(element.Value);  
            }  
            else  
            {  
                string attbriutename = "";  
                if(element.HasAttributes)  
                {  
                    attbriutename = element.Name.ToString();  
                    foreach (var item in element.Attributes())  
                    {  
                        attbriutename += " "+item.ToString();  
                    }  
                }  
                else  
                {  
                    attbriutename = element.Name.ToString();  
                }  
                var node = parent.Add(attbriutename);  
                //node.Tag = element;  
                foreach (var child in element.Elements())  
                {  
                    AddNodes(child, node.Nodes);  
                  
                }  
    
            }  
        }  
    

    Result in xml and treeview:

    205630-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful