1

I would like to display the content of an XmlDocument in a TreeView. I have created an XMLDataProvider from the XMLDocument:

Parent = new XmlDataProvider() { Document = e.XsdData, XPath = "*" };

and bound the Parent to the TreeView. I am using a HierarchicalDataTemplate to display the name of a node:

<TreeView Name="treeView" ItemsSource="{Binding Parent}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="xs:simpleType"
                                  ItemsSource="{Binding XPath=xs:element}">
            <TextBlock Text="{Binding XPath=name}"/>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

This is a section from my XML File I have loaded:

<?xml version="1.0"?>
<xs:schema xmlns:cfg="syncAXIS" elementFormDefault="qualified" targetNamespace="syncAXIS" version="0.5" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="LogConfigType">
    <xs:all>
      <xs:element name="LogfilePath" type="xs:string" />
      <xs:element name="Loglevel">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="Error" />
            <xs:enumeration value="Warn" />
            <xs:enumeration value="Info" />
            <xs:enumeration value="Debug" />
            <xs:enumeration value="Trace" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="EnableConsoleLogging" type="xs:boolean" />
      <xs:element name="EnableFilelogging" type="xs:boolean" />
      <xs:element name="MaxLogfileSize" type="xs:positiveInteger" />
      <xs:element name="MaxBackupFileCount" type="xs:nonNegativeInteger" />
    </xs:all>
  </xs:complexType>
  <xs:simpleType name="IPAddressType">
    <xs:restriction base="xs:string">
      <xs:pattern value="(([01][0-9][0-9]|2[0-4][0-9]|25[0-5]|[0-9][0-9]|[0-9])\.){3}([01][0-9][0-9]|2[0-4][0-9]|25[0-5]|[0-9][0-9]|[0-9])" />
    </xs:restriction>
  </xs:simpleType>

The TreeView is not updated / never displayed. The DataContext is correctly set to my ViewModel:

private XMLEditorViewModel viewModel;
public MainWindow()
{
    viewModel = new XMLEditorViewModel();
    DataContext = viewModel;
    InitializeComponent();
}
FredM
  • 454
  • 9
  • 20
laserman
  • 233
  • 2
  • 10
  • No it didn't help me. I have the feeling the XmlDAtaProvider is not updating the Binding in the Window. But I thought it works exactly like an ObservableCollection. – laserman Jan 10 '18 at 11:50
  • See following posting : https://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Jan 10 '18 at 14:33
  • That is a forms application. I am using WPF. – laserman Jan 10 '18 at 15:47
  • Still the code shows how to recursively parse the xml. The write commands to the Tree will be slightly different. – jdweng Jan 10 '18 at 17:40
  • No need to recursively parse the xml - TreeView in WPF can directly display XMLDocument (see below) – laserman Jan 10 '18 at 18:46

1 Answers1

1

The solution is

  1. Use TreeView.DataTemplate instead of TreeView.Resources
  2. Use ItemsSource=Parent.Document instead of ItemsSource=Parent

laserman
  • 233
  • 2
  • 10