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)
XAML:
C#:
public class Category
{
public string Name { get; set; }
public List
public Uri Url { get; set; }
}
public partial class Page : UserControl
{
private List
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
{
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
Replies
Know the answer? Post it — somebody with the same question will find it here.