I'm searching for hours and I'm trying to bind a XML-File to a treeView. In general i want to select a XML-File in a ListBox an Open the XML File in a Treeview.
The XML looks like this:
<test>
<communication>
<global>
<server id="Server" ip="172.17.10.50" port="5072" />
</global>
<devices>
<device id="Server" ip="172.18.100.50" port="3451" />
<device id="cInterface" ip="172.17.12.52" port="3567" />
<device id="caServer" ip="172.17.12.52" port="3000" />
</devices>
<groups>
<group id="group_cInterface">
<device id="cInterface" />
<device id="Server" />
</group>
</groups>
</communication>
</test>
Thanks a lot. If my whole code is needed, feel free to ask!
EDIT:
Could reach this output: Output
with this XAML Code:
<Window x:Class="Communication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="693.55" Width="780">
<Window.Resources>
<HierarchicalDataTemplate x:Key="NodeTemplate"
ItemsSource="{Binding XPath=./*}">
<TextBlock x:Name="nodetext"/>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}"
Value="Element">
<Setter TargetName="nodetext" Property="Text"
Value="{Binding Path=Name}" />
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<Button Content="loadData" HorizontalAlignment="Left" Height="31" Margin="10,246,0,0" VerticalAlignment="Top" Width="63" Click="Button_Click"/>
<ListBox x:Name="listBoxKonsole" HorizontalAlignment="Left" Height="214" Margin="10,27,0,0" VerticalAlignment="Top" Width="752" SelectionChanged="listBoxKonsole_SelectionChanged" />
<TreeView x:Name="treeView" Height="214" Margin="10,282,0,0" Width="752" ItemTemplate="{StaticResource NodeTemplate}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding}"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Standard" />
</Grid>
</Window>
and this code:
public void listBoxKonsole_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the file's location.
string filename = listBoxKonsole.SelectedItem.ToString();
// Make an XmlDataProvider that uses the file.
XmlDataProvider provider = new XmlDataProvider();
provider.Source = new Uri(filename, UriKind.Absolute);
provider.XPath = "./*";
// Make the TreeView display the XmlDataProvider's data.
treeView.DataContext = provider;
}
Now my Pproblem is that i also want to show the xml Attributes and not only the XML Node Names. The attributes can be in one line with the name of the node but i dont know how to realize it.
Thanks