New to Telerik UI for ASP.NET MVCStart a free 30-day trial

Binding the TreeView to XML Data

Environment

ProductTelerik UI for ASP.NET MVC TreeView
Product VersionCreated with version 2024.4.1112

Description

How can I bind the TreeView component to XML in ASP.NET MVC application?

Solution

The example below uses the System.Xml.Linq library to convert the XML structure into C# models. The models represent the hierarchical data from the XML file and are then used to populate the TreeView data. Binding data from an XML source can be implemented using the following approaches:

  • Server Binding
Razor
@model IEnumerable<Telerik.Examples.Mvc.Areas.TreeViewBindingToXml.Models.Employee>

@(Html.Kendo().TreeView()
    .Name("serverTree")
    .BindTo(Model, (Kendo.Mvc.UI.Fluent.NavigationBindingFactory<TreeViewItem> mappings) =>
    {
        mappings.For<Telerik.Examples.Mvc.Areas.TreeViewBindingToXml.Models.Employee>(bound => bound.ItemDataBound((node, employee) =>
        {
            node.Text = employee.name;
            node.Id = employee.id.ToString();
        })
        .Children(e => e.items));
    })
)
  • AJAX Binding
Razor
@(Html.Kendo().TreeView()
    .Name("ajaxTree")
    .DataTextField("name")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("Employees", "Home");
        });
    })
)

To see the complete example, refer to the binding the TreeView to XML data in the UI for ASP.NET MVC Examples repository.

More ASP.NET MVC TreeView Resources

See Also