Live Chat Icon For mobile
Live Chat Icon

How do I bind my XML Data to the tree (the xml contains multi-level hierarchical data with leaf nodes at many levels)?

Platform: WPF| Category: TreeView

Here is a sample XML and how it gets bound to a tree. The XML is of the form:


See how there can be any number of Category levels and the Reports can appear at any level too.

Here is the sample code to bind this to the tree: (For better code formatting take a look at this incident in the MSDN Forums: A xml string…need to place nodes in a treeview.

        <XmlDataProvider x:Key='myReportsData' XPath='CATEGORY'  IsInitialLoadEnabled='True'>   
                <CATEGORY NAME='main 1' xmlns=''>   
                    <CATEGORY NAME='sub main 1'>   
                        <REPORT NAME='report111'>   
                    <CATEGORY NAME='test2222'>   
                        <CATEGORY NAME='test3333'>   
                            <REPORT NAME='report_test222'>   
                        <CATEGORY NAME='test555'>   
                            <REPORT NAME='report_test333'>   
        <HierarchicalDataTemplate DataType='CATEGORY'  ItemsSource ='{Binding XPath=*}'>   
            <TextBlock Text='{Binding XPath=@NAME}' />  
        <DataTemplate DataType='REPORT'>   
            <TextBlock Text='{Binding XPath=@NAME}' />  
    <TreeView Name='myTreeView' Background='Aqua' >  
            <Binding Source='{StaticResource myReportsData}'/>   

Share with