Loading a Treeview from XML

Posted on 2006-06-02
Last Modified: 2013-11-19
Good morning experts:

I would appreciate any help you could offer in the area of loading a treeview from an XML document.  Here is an example XML document:

<?xml version="1.0" encoding="utf-8" ?>

I would like to make the Name element the Node text and the ID element the Node tag.  I am currently using this code to populate the nodes, but I'm not sure how to make the first element the tag and the second element the node text.

private: System::Void frmComponents_Load(System::Object^  sender, System::EventArgs^  e) {
                          // Declare a variable of type  XmlTextReader
                   XmlTextReader ^xtr;
                   // Declare a string that holds the name of the file
                   String ^fileName = "Components.xml";
try {
                         // Initialize the XmlTextReader variable with the name of the file
                         xtr = gcnew XmlTextReader(fileName);
                         xtr->WhitespaceHandling = WhitespaceHandling::None;

                         // Scan the XML file
                         while (xtr->Read())
                               // every time you find an element, find out what type it is
                               switch (xtr->NodeType)
                               case XmlNodeType::Text:

                         // Close the XmlTextReader


Thanks in advance
Question by:escheider
    LVL 15

    Expert Comment

    Do you mean TreeNode's Tag?
    You can set the value for the TreeNode->Tag property.
    this->tvComponents->Nodes[index]->Tag = Value;
    Please refer to the link for more details
    LVL 4

    Author Comment

    Yes, I meant the tree node's tag; however, the way the data is structured in the XML file, I'm not sure how to seperate each element to do this in one pass.    Perhaps I'm using the wrong approach, but I'd like to have text displayed for each node and then an identifier to make each node unique.  In the XML I provided, this would be the ID field and the text to be displayed would be the NAME field.
    LVL 15

    Accepted Solution

    Handle this case

    case XmlNodeType::Element:
      bIsID = true;

     case XmlNodeType::Text:
            this->tvComponents->Nodes[index-1]->Tag = xtr->Value;
            bIsID = false;

    bool bIsID = false
    int index = 0
    outside the while loop.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now