• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Loading a Treeview from XML

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" ?>
<ComponentTypes>
      <Component>
            <ID>0</ID>
            <Name>Text</Name>
      </Component>
      <Component>
            <ID>1</ID>
            <Name>Date</Name>
      </Component>
</ComponentTypes>

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:
                                     this->tvComponents->Nodes->Add(xtr->Value);
                                     break;
                               }      
                         }

                   }
                   __finally
                   {
                         // Close the XmlTextReader
                               xtr->Close();
                   }

                   }


Thanks in advance
0
escheider
Asked:
escheider
  • 2
1 Solution
 
lakshman_ceCommented:
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
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx
0
 
escheiderAuthor Commented:
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.
0
 
lakshman_ceCommented:
Handle this case

case XmlNodeType::Element:
if((xtr->Name)->Equals("ID"))
  bIsID = true;

 case XmlNodeType::Text:
     index++;
     this->tvComponents->Nodes->Add(xtr->Value);
     if(bIsID)
     {
        this->tvComponents->Nodes[index-1]->Tag = xtr->Value;
        bIsID = false;
      }
     break;

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now