Generating Tree View Nodes From XML File In Silverlight 3 Application

No.of Views1444
Bookmarked0 times
Downloads 
Votes0
By  dpatra   On  16 Feb 2010 00:02:01
Tag : Silver Light and XAML , How to
Generating Tree View Nodes From XML File In Silverlight 3 Application
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

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

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

Image Loading...

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

Image Loading...

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

Image Loading...

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

Image Loading....

Add to Properties as Name and SubCategory.
{codecitation class="brush: csharp; gutter: true;" width="650px"}


public class Category
{
public string Name { get; set; }
public List<Category> SubCategory { get; set; }
}


{/codecitation}

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


Image Loading...


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


{codecitation class="brush: xml; gutter: true;" width="650px"}
<?xml version="1.0" encoding="utf-8" ?>
<categories>
<category name="Panels">
<category name="Grid"></category>
<category name="Border"></category>
<category name="Stack Panel"></category>
<category name="Canvas"></category>
<category name="Wrap Panel"></category>
</category>
<category name="Shape">
<category name="Rectangle"></category>
<category name="Elipse"></category>
<category name="Line"></category>
</category>
<category name="Others">
<category name="Button"></category>
<category name="Check Box"></category>
<category name="Radio Button"></category>
<category name="Text Block"></category>
<category name="Text Box"></category>
<category name="Password Box"></category>
</category>
</categories>


{/codecitation}

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

{codecitation class="brush: csharp; gutter: true;" width="650px"}

public MainPage()
{
InitializeComponent();
LoadData();
}

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

private List<Category> GetCategories(XElement element)
{
return (from category in element.Elements("category")
select new Category()
{
Name = category.Attribute("name").Value,
SubCategory = this.GetCategories(category)
}).ToList();
}



{/codecitation}

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.

{codecitation class="brush: xml; gutter: true;" width="650px"}

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

{/codecitation}

Now the xaml for TreeView Data Template:

{codecitation class="brush: xml; gutter: true;" width="650px"}

<controls:TreeView x:Name="MyTreeView" Height="200" VerticalAlignment="Center" Margin="0" Background="#FFF6E9BB" HorizontalAlignment="Center" Width="200">
<controls:TreeView.ItemTemplate>
<myControl:HierarchicalDataTemplate ItemsSource="{Binding SubCategory}">
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</myControl:HierarchicalDataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>


{/codecitation}

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

{codecitation class="brush: csharp; gutter: true;" width="650px"}

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



{/codecitation}

Now go ahead and run your application.

Image Loading....

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

Thank you


About the Author


Diptimaya Patra

Description :I am a Master in Computer Application (MCA) from SRM University, Chennai. I am MCTS in ASP.Net Web Development, and MOSS 2007 Administration. I have extreme exposure to Microsoft Technologies in recent times like Silverlight 2, Silverlight 3. I am from Cuttack, Orissa. You can reach me using this mail (diptimaya.patra@gmail.com). Currently I am working as a Software Engineer in UST Global Inc in Trivandrum Center.

Occupation :Software Engineer
Company : UST Global.
Location : India
Follow me at twitter : http://twitter.com/dpatra


 
Sign Up to vote for this article
 
About Author
 
dpatra
Occupation-Not Provided
Company-Not Provided
Member Type-Expert
Location-Not Provided
Joined date-13 Jul 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top