gpt4 book ai didi

c# - 将 XML 文件显示到列表框中

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:56 24 4
gpt4 key购买 nike

我是 WPF/C# 编程的新手。我正在尝试使用此 XAML 代码将 xml 文件内容显示到列表框中:

<Window x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="HostsData"
Source="/Hosts.xml"
XPath="Hosts/Host" />
</Window.Resources>
<Grid>
<ListBox Height="100" HorizontalAlignment="Left" Margin="98,70,0,0" Name="listBox1"
VerticalAlignment="Top" Width="120" SelectionChanged="listBox1_SelectionChanged"
ItemsSource="{Binding Source={StaticResource HostsData}}"
DisplayMemberPath="HostName"/>
</Grid>
</Window>

Hosts.xml 包含:

<Hosts>
<Host>
<IP>1.1.1.1</IP>
<HostName>abc01</HostName>
</Host>
<Host>
<IP>2.2.2.2</IP>
<HostName>abc02</HostName>
</Host>
</Hosts>

我构建成功,但是当我运行应用程序时,列表框是空的!我已将 Hosts.xml 文件复制到各处,但仍然没有。

有什么想法吗?

最佳答案

我试过这样....看看

<Window x:Class="WpfApplication2.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="300" Width="300">
<Window.Resources>
<XmlDataProvider x:Key="BookmarkData" XPath="Hosts/Host">
<x:XData>
<Hosts>
<Host>
<IP>1.1.1.1</IP>
<HostName>abc01</HostName>
</Host>
<Host>
<IP>2.2.2.2</IP>
<HostName>abc02</HostName>
</Host>
</Hosts>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<ListBox
Background="#999"
BorderThickness="2"
BorderBrush="White"
Margin="10"
DisplayMemberPath="HostName"
ItemsSource="{Binding Source={StaticResource BookmarkData}, XPath=/Hosts/Host}"
/>

</Grid>

关于c# - 将 XML 文件显示到列表框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5607741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com