数据绑定(六)使用XML数据作为Binding的Source

.NET Framework提供了两套处理XML数据的类库

1. 符合DOM标准的类库:包括XmlDocument、XmlElement、XmlNode、XmlAttribute等类,这套类库的特定是中规中矩、功能强大,但也背负了太多XML的传统和复杂

2. 已LINQ为基础的类库:包括XDocument、XElement、XNode、XAttribute等类,这套类库的特点是可以使用LINQ进行查询和操作,方便快捷

xml文件内容如下:

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <StudentList>  
  3.   <Student id="1">  
  4.     <Name>Tim</Name>  
  5.   </Student>  
  6.   <Student id="2">  
  7.     <Name>Tom</Name>  
  8.   </Student>  
  9.   <Student id="3">  
  10.     <Name>Vina</Name>  
  11.   </Student>  
  12.   <Student id="4">  
  13.     <Name>Emily</Name>  
  14.   </Student>  
  15. </StudentList>  


界面代码如下:

 

 

[html] view plain copy
 
  1. <Window x:Class="WpfApplication1.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:WpfApplication1"  
  5.         Title="MainWindow" Height="244" Width="412">  
  6.     <StackPanel Background="LightBlue">  
  7.         <ListView x:Name="listViewStudents" Height="130" Margin="5">  
  8.             <ListView.View>  
  9.                 <GridView>  
  10.                     <GridViewColumn Header="Id" Width="80" DisplayMemberBinding="{Binding XPath=@id}" />  
  11.                     <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding XPath=Name}" />  
  12.                 </GridView>  
  13.             </ListView.View>  
  14.         </ListView>  
  15.         <Button Content="Load" Click="Button_Click" Height="25" Margin="5, 0" />  
  16.     </StackPanel>  
  17. </Window>  


其中,两列分别绑定到XPath上

 

按钮的点击处理代码如下:

 

[csharp] view plain copy
 
  1. XmlDocument doc = new XmlDocument();  
  2. doc.Load("Students.xml");  
  3.   
  4. XmlDataProvider xdp = new XmlDataProvider();  
  5. xdp.Document = doc;  
  6. xdp.XPath = @"/StudentList/Student";  
  7.   
  8. listViewStudents.DataContext = xdp;  
  9. listViewStudents.SetBinding(ListView.ItemsSourceProperty, new Binding());  


代码定义了一个XmlDataProvider对象,使用XmlDataProvider对象读取一个xml文件,可以通过load方法加载一个xml,也可以通过对它的Source属性赋一个Uri来加载一个网络上的xml,例如:

 

 

[csharp] view plain copy
 
  1. xdp.Source = new Uri(@"d:\Students.xml")  
[csharp] view plain copy
 
  1. <span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240); ">XmlDataProvider对象的XPath属性通过一个XPath指定了所有目标元素的路径</span>  


运行效果如图:

 


xml数据可以作为线性数据的数据源,也可以用于展现树形数据结构,界面代码如下:

 

[html] view plain copy
 
  1. <Window x:Class="WpfApplication1.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:local="clr-namespace:WpfApplication1"  
  5.         Title="MainWindow" Height="244" Width="412">  
  6.     <Window.Resources>  
  7.         <XmlDataProvider x:Key="xdp" XPath="FileSystem/Folder">  
  8.             <x:XData>  
  9.                 <FileSystem xmlns="">  
  10.                     <Folder Name="Books">  
  11.                         <Folder Name="Programming">  
  12.                             <Folder Name="Windows">  
  13.                                 <Folder Name="WPF" />  
  14.                                 <Folder Name="MFC" />  
  15.                                 <Folder Name="Delphi" />  
  16.                             </Folder>  
  17.                         </Folder>  
  18.                         <Folder Name="Tools">  
  19.                             <Folder Name="Development" />  
  20.                             <Folder Name="Designment" />  
  21.                             <Folder Name="Players" />  
  22.                         </Folder>  
  23.                     </Folder>  
  24.                 </FileSystem>  
  25.             </x:XData>  
  26.         </XmlDataProvider>  
  27.     </Window.Resources>  
  28.     <Grid>  
  29.         <TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=xdp}}">  
  30.             <TreeView.ItemTemplate>  
  31.                 <HierarchicalDataTemplate ItemsSource="{Binding XPath=Folder}">  
  32.                     <TextBlock Text="{Binding XPath=@Name}" />  
  33.                 </HierarchicalDataTemplate>  
  34.             </TreeView.ItemTemplate>  
  35.         </TreeView>  
  36.     </Grid>  
  37. </Window>  


运行效果如图:

 

 

posted on 2017-07-26 09:13  alex5211314  阅读(185)  评论(0编辑  收藏  举报

导航