Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tree View Control To XML 1

Status
Not open for further replies.

ratzp

Programmer
Dec 30, 2005
49
IN
Can anybody help me out...

I have a TreeView Control which has Nodes and ChildNodes

I want to convert it into XML

What is the easiest way to do it..

Ratz
 
usually the treeview is populated from an xml document or database source. I would start at the source of the data and convert this into xml.

if you need to pull data from the GUI control (requires user input?) then loop through the control, extract the values you want and place them in an XMLDocument object. Save the XMLDocument.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks jmeckley,

I am adding or populatin the Treeview from the GUI

Can you write a sample code for creating XML Node and Child Nodes... using XMLDocument and XMLNode etc (with Attributes)



ratz


 
is this a windows or web control?
what does the tree data look like?
what do you want the xml file to look like?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
its a window control... I want to save it an xml File
For Eg:
TreeView
Root
- ----- ChildNode
- ChildNode1
- ----- ChildNode3
- ChildNode4

XML
<Root>
<ChildNode><ChildNode1/></ChildNode>
<ChildNode3><ChildNode4/><ChildNode3>
</Root>
 
I am not fimiliar with the exact properties of the Desktop TreeView control, but it would look something like this:
Code:
private XMLDocument xDoc = null;
protected void [MyEvent](object sender, EventArgs e)
{
   if(xDoc == null)
   {
      this.xDoc = new XMLDocument();
      this.xDoc.LoadXML("<Root/>");
      xDoc.AppendChild(AppendTreeViewNode(this.myTreeView.TopNode));
      xDoc.Save("filename.xml");
   }
}

private XmlNode AppendTreeViewNode(TreeViewNode treeNode)
{
    XmlNode toReturn = this.xDoc.CreateNode(NodeType.Element);
    XmlAttribute text = this.xDoc.CreateNode(NodeType.Attribute);
    XmlAttribute value = this.xDoc.CreateNode(NodeType.Attribute);

    text.Name = "DisplayText";
    text.Value = treeNode.Text;

    value.Name = "Value";
    value.Value = treeNode.Value;

    toReturn.Attributes.Add(text);
    toReturn.Attributes.Add(value);

    foreach(TreeViewNode child in treeNode.Children)
    {
         toReturn.AppendChild(AppendTreeViewNode(child));
    }

    return toReturn;
}
for more information check out the System.XML and System.Windows.Forms.TreeView namespace

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top