escheider
asked on
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
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:
// 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-
break;
}
}
}
__finally
{
// Close the XmlTextReader
xtr->Close();
}
}
Thanks in advance
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can set the value for the TreeNode->Tag property.
this->tvComponents->Nodes[
Please refer to the link for more details
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx