# Binding the TreeView to XML Data

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC TreeView</td>
 </tr>
 <tr>
  <td>Product Version</td>
  <td>Created with version 2024.4.1112</td>
 </tr>
</table>

## Description

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

## Solution

The example below uses the [`System.Xml.Linq`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq?view=netframework-4.8.1) 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 HtmlHelper
@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));
    })
)
```

```C# Controller
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        XElement element = XElement.Load(Server.MapPath("~/App_Data/employees.xml"));
        List<Employee> employees = ToEmployees(element);
        return View(employees);
    }

    private List<Employee> ToEmployees(XElement element)
    {
        if (element != null && element.HasElements)
        {
            List<Employee> employees = new List<Employee>();
            foreach (var child in element.Elements("Employee"))
            {
                var employee = ToEmployee(child);
                if (employee.hasChildren)
                {
                    employee.items = ToEmployees(child.Element("items"));
                }
                employees.Add(employee);
            }
            return employees;
        }
        return null;
    }
}
```

* AJAX Binding

```Razor HtmlHelper
@(Html.Kendo().TreeView()
    .Name("ajaxTree")
    .DataTextField("name")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("Employees", "Home");
        });
    })
)
```

```C# Controller
    public class HomeController : Controller
    {
        public JsonResult Employees(string id)
        {

            XElement element = XElement.Load(Server.MapPath("~/App_Data/employees.xml"));
            IEnumerable<Employee> result;

            if (!string.IsNullOrEmpty(id))
            {
                // Search for "id" and return it's child items.
                result = FindByID(id, element.Element("Employee")).Element("items").Elements("Employee").Select(e => ToEmployee(e));
            }
            else
            {
                // Return first level nodes.
                result = element.Elements("Employee").Select(e => ToEmployee(e));
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

        private XElement FindByID(string id, XElement element)
        {
            if (element.Element("employeeId").Value == id)
            {
                return element;
            }

            if (element.Element("items") != null)
            {
                XElement item;
                foreach (var child in element.Element("items").Elements("Employee"))
                {
                    item = FindByID(id, child);
                    if (item != null)
                    {
                        return item;
                    }
                }
            }
            return null;
        }

        private Employee ToEmployee(XElement element)
        {
            return new Employee()
            {
                id = int.Parse(element.Element("employeeId").Value),
                name = element.Element("name").Value,
                hasChildren = element.Element("items") != null
            };
        }
    }
```

To see the complete example, refer to the [binding the TreeView to XML data](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/TreeViewBindingToXml) in the [UI for ASP.NET MVC Examples repository](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master).

## More ASP.NET MVC TreeView Resources

* [ASP.NET MVC TreeView Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/navigation/treeview/overview)

* [ASP.NET MVC TreeView Demos](https://demos.telerik.com/aspnet-mvc/treeview)

* [ASP.NET MVC TreeView Product Page](https://www.telerik.com/aspnet-mvc/panelbar)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the TreeView for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/treeview)
* [Server-Side API Reference of the TreeView for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/treeview)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
