New to Telerik UI for WinForms? Download free 30-day trial

Binding to XML Data

RadTreeView can consume XML data both by directly loading it (see Serialize/Deserialize to XML) or by loading it to a DataSet for binding. The following example uses a sample "Table of contents" XML. Each node element in the table of contents has a "Title" element, "id" and "parentid".

    <Toc>
      <Node>
        <Title>Main Title</Title>
        <id>1</id>
        <parentId></parentId>
      </Node>
      <Node>
        <Title>Child Title</Title>
        <id>2</id>
        <parentId>1</parentId>
      </Node>
    </Toc>

Assuming that "toc.xml" is in the TreeView\DataBinding directory which is relative to the directory of the the executable, the following code can be run from the form Load event handler. A new DataSet object is created and the DataSet. ReadXml() method consumes the sample data. The element being shown is the DisplayMember property "Title". The ValueMember property is set to the "id" element and it will be available in the Value property of each RadTreeNode at runtime. The ParentMember and ChildMember properties define the relation between the records in the data source object:

WinForms RadTreeView Binding to XML Data

DataSet tocDataSet = new DataSet("Toc");
tocDataSet.ReadXml("TreeView\\DataBinding\\toc.xml");
this.radTreeView1.DataMember = "Node";
this.radTreeView1.DisplayMember = "Title";
this.radTreeView1.ChildMember = "id";
this.radTreeView1.ParentMember = "parentId";
this.radTreeView1.DataSource = tocDataSet;

Dim tocDataSet As DataSet = New DataSet("Toc")
tocDataSet.ReadXml("TreeView\DataBinding\toc.xml")
Me.RadTreeView1.DataMember = "Node"
Me.RadTreeView1.DisplayMember = "Title"
Me.RadTreeView1.ChildMember = "id"
Me.RadTreeView1.ParentMember = "parentId"
Me.RadTreeView1.DataSource = tocDataSet

See Also

In this article