I would like to display arbitrary XML in a TreeView with extension and collapse of nodes, displaying both the name of the element and the set of attributes and their values. I think I can do this with the HierarchicalDataTemplate.
I saw tips for using the HierarchicalDataTemplate to display arbitrary XML elements and text nodes, for example:
<Window.Resources> <HierarchicalDataTemplate x:Key="NodeTemplate"> <TextBlock x:Name="tbName" Text="?" /> <HierarchicalDataTemplate.ItemsSource> <Binding XPath="child::node()" /> </HierarchicalDataTemplate.ItemsSource> <HierarchicalDataTemplate.Triggers> <DataTrigger Binding="{Binding Path=NodeType}" Value="Text"> <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value}"/> </DataTrigger> <DataTrigger Binding="{Binding Path=NodeType}" Value="Element"> <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/> </DataTrigger> </HierarchicalDataTemplate.Triggers> </HierarchicalDataTemplate> <XmlDataProvider x:Key="xmlDataProvider"> </XmlDataProvider> </Window.Resources> .... <TreeView Name="treeView1" ItemsSource="{Binding Source={StaticResource xmlDataProvider}, XPath=*}" ItemTemplate= "{StaticResource NodeTemplate}"/>
Which works great. It displays element names and text for each element. But my XML uses attributes to carry information. The schema is complex and I donโt have a formal definition for it, so I consider it as arbitrary XML.
The simplest document is as follows:
<c4soap name="GetVersionInfo" seq="" result="1"> <versions> <version name="Director" version="2.1.0.126418" buildtype="" builddate="Jun 1 2011" buildtime="14:52:43" /> <version name="MediaManager" version="2.1.0.126418" buildtype="" builddate="Jun 1 2011" buildtime="14:36:17" /> </versions> </c4soap>
Using the above definition of HierarchicalDataTemplate, I get this for display:

Not quite what I want. For each node, I want to display both the name of the element and a set of attributes and their values.
I tried this:
<Window.Resources> <HierarchicalDataTemplate x:Key="NodeTemplate"> <WrapPanel Focusable="False"> <TextBlock x:Name="tbName" Text="?" /> <TextBlock x:Name="tbAttrs" Text="?" /> </WrapPanel> <HierarchicalDataTemplate.ItemsSource> <Binding XPath="child::node()" /> </HierarchicalDataTemplate.ItemsSource> <HierarchicalDataTemplate.Triggers> <DataTrigger Binding="{Binding Path=NodeType}" Value="Text"> <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value}"/> </DataTrigger> <DataTrigger Binding="{Binding Path=NodeType}" Value="Element"> <Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}"/> <Setter TargetName="tbAttrs" Property="Text" Value="{Binding Path=Attributes}"/> </DataTrigger> </HierarchicalDataTemplate.Triggers> </HierarchicalDataTemplate> <XmlDataProvider x:Key="xmlDataProvider"> </XmlDataProvider> </Window.Resources>
... which brings me a little closer, but Value="{Binding Path=Attributes}"
leads to displaying "(Collection)" in the TreeView.

How can I just display all the actual names and attribute values โโin addition to the element name?