Hi,
I am having an XML file, the entire content of an XML file is in XDocument now i want to bind it a TreeView.   Suggest a way which we can directly assign as a source to a TreeView and then DataBind().  But i dont want to use the below mentioned way:
XML: (Data.xml)

  http://www.microsoft.com">
   
      http://msdn.microsoft.com">
       
     

      http://www.silverlight.net">
       
          http://silverlight.net/forums">
           
         

       

     

   

 

 
   
      http://www.live.com">
       
     

      http://www.google.com">
       
     

      http://www.yahoo.com">
       
     

   

 


XAML:
       
           
               
                   
               

           

       

C#:
    public class Category
    {
        public string Name              { get; set; }
        public List Children  { get; set; }
        public Uri Url                  { get; set; }
    }
    public partial class Page : UserControl
    {
        private List Categories { get; set; }
        public Page()
        {
            InitializeComponent();
            // Populate categories from local data source
            XElement root = XElement.Load("Data.xml");
            Categories = (from c in root.Elements("category")
                          select new Category
                          {
                              Name = (string)c.Attribute("name"),
                              Children = LoadData(c),
                              Url = ((string)c.Attribute("url") == "") ? null : new Uri((string)c.Attribute("url"), UriKind.RelativeOrAbsolute)
                          }).ToList();
            // Update categories' data
            listCategories.ItemsSource = Categories;              
        }
        private List LoadData(XElement root)
        {
            if (root == null)
                return null;
            // Load children data
            return (from c in root.Element("children").Elements("category")
                    select new Category
                    {
                        //Create source
                        Name = (string)c.Attribute("name"),
                        Children = c.Element("children") == null ? null : LoadData(c),
                        Url = ((string)c.Attribute("url") == "") ? null : new Uri((string)c.Attribute("url"), UriKind.RelativeOrAbsolute)
                    }).ToList();
        }
        private void listCategories_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            Category category = (Category)listCategories.SelectedItem;
            // Validate category (some categories don't have files)           
            if (category == null || category.Url == null)
                return;
            HtmlPage.Window.Navigate(category.Url, "_blank");
        }
    }
 
Thanks in Advance.

Replies

Know the answer? Post it — somebody with the same question will find it here.