.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Create Tree View Nodes From XML File In Silverlight Application

Posted By:Diptimaya Patra       Posted Date: April 14, 2010    Points: 25    Category: Silverlight    URL: http://www.dotnetspark.com  

In this article we will see how can we generate TreeView Nodes based on an XML file in silverlight application.
 

Introduction

In this article we will see how can we generate TreeView Nodes based on an XML file.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a Silverlight Application. Name it as TreeViewXMLInSL3.

pic1.gif

Go ahead and add a Tree View control to your application. You can find Tree View from the Asset Library. As shown below:

pic2.gif

I have given the background of the Tree View for distinguishing.

pic3.gif

Now create a Class called Category.cs and add it to the Silverlight Project.

pic4.gif

Add to Properties as Name and SubCategory.
public class Category
{
  public string Name { get; set; }
  public List SubCategory { get; set; }
}


Now add an XML file to the Silverlight Project and name it as "Categories.xml".

Write the XML in the following format with the following Data:

pic5.gif



  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  


Now in the MainPage.xaml find the constructor and add the following code below InitializeComponent();

public MainPage()
{
InitializeComponent();
LoadData();
}
private void LoadData()
{
List categories = new List();
XDocument categoriesXML = XDocument.Load("Categories.xml");
categories = this.GetCategories(categoriesXML.Element("categories"));
}
private List GetCategories(XElement element)
{
return (from category in element.Elements("category")
select new Category()
{
Name = category.Attribute("name").Value,
SubCategory = this.GetCategories(category)
}).ToList();
}

Remember if you cannot find XDocument use the assembly System.Xml.Linq;

Now we will define the Item Template in Tree View. Go ahead in the XAML code behind and change the Tree View Template as following:

Don't forget to refer to the following assembly in xaml.

xmlns:myControl="clr-namespace:System.Windows;assembly=System.Windows.Controls"

Now the xaml for TreeView Data Template:

  
  
  
  
  
  
  


Now at last you need to give the ItemsSource of the TreeView. Add the following code in the method LoadData()

    private void LoadData()
    {
        List categories = new List();
        XDocument categoriesXML = XDocument.Load("Categories.xml");
        categories = this.GetCategories(categoriesXML.Element("categories"));
        this.MyTreeView.ItemsSource = categories;
    }


Now go ahead and run your application.

pic6.gif

You have successfully generated the Tree View nodes from an XML file.

Enjoy Coding.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend