Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将XML文件显示到列表框中_C#_Xml_Wpf_Binding - Fatal编程技术网

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

C# 将XML文件显示到列表框中,c#,xml,wpf,binding,C#,Xml,Wpf,Binding,我是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" Heigh

我是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>

1.1.1.1
abc01
2.2.2.2
abc02
我构建成功,但当我运行应用程序时,列表框为空!我到处都复制了Hosts.xml文件,但还是什么也没有


有什么想法吗?

我想你忘了指定数据上下文

DataContext="{Binding Source={StaticResource HostsData}}
作为网格xaml属性或Listbox

由于数据来自XML提供程序,请尝试使用Xpath属性标记

编辑:

我发现发布一个完整的示例实现更相关,请忽略特定于混合的名称空间

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Screen_2_1_Name"
mc:Ignorable="d"
x:Class="WpfPrototype1Screens.Screen_2_1"
Width="640" Height="480">
<UserControl.Resources>
    <XmlDataProvider x:Key="uneDataSource" Source="http://www.lemonde.fr/rss/une.xml" d:IsDataSource="True"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource uneDataSource}}">
    <ListBox Margin="80,88,64,112" Style="{DynamicResource ListBox-Sketch}" ItemsSource="{Binding XPath=/rss/channel/item/title}"/>
</Grid>


我想您忘记指定数据上下文了

DataContext="{Binding Source={StaticResource HostsData}}
作为网格xaml属性或Listbox

由于数据来自XML提供程序,请尝试使用Xpath属性标记

编辑:

我发现发布一个完整的示例实现更相关,请忽略特定于混合的名称空间

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="Screen_2_1_Name"
mc:Ignorable="d"
x:Class="WpfPrototype1Screens.Screen_2_1"
Width="640" Height="480">
<UserControl.Resources>
    <XmlDataProvider x:Key="uneDataSource" Source="http://www.lemonde.fr/rss/une.xml" d:IsDataSource="True"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource uneDataSource}}">
    <ListBox Margin="80,88,64,112" Style="{DynamicResource ListBox-Sketch}" ItemsSource="{Binding XPath=/rss/channel/item/title}"/>
</Grid>


我试过这样。。。。看看

<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>

1.1.1.1
abc01
2.2.2.2
abc02

我试过这样。。。。看看

<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>

1.1.1.1
abc01
2.2.2.2
abc02

我刚刚测试了相同的代码,没有SelectionChanged属性,列表框显示了2个元素。“我到处复制Hosts.xml文件”是什么意思?当我将xml文件添加到项目中时,它工作正常,内容已列出。但这不是我想要的,我的意思是我想使用一个外部XML文件,外部XML文件存储在哪里?您只需要为XmlDataProvider提供一个有效的源代码。例如,这可能是本地文件或URL的路径。我刚刚测试了相同的代码,没有SelectionChanged属性,列表框显示了2个元素。“我到处复制Hosts.xml文件”是什么意思?当我将xml文件添加到项目中时,它工作正常,内容已列出。但这不是我想要的,我的意思是我想使用一个外部XML文件,外部XML文件存储在哪里?您只需要为XmlDataProvider提供一个有效的源代码。例如,可以是本地文件或URL的路径。