using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml; namespace ReadXml_1005 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Button_Click4(object sender, RoutedEventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load("RawData.xml"); XmlDataProvider xdp = new XmlDataProvider(); xdp.Document = doc; xdp.XPath = @"/StudentList/Student"; this.listViewStudents2.DataContext = xdp; this.listViewStudents2.SetBinding(ListView.ItemsSourceProperty, new Binding()); } } }MainWindow.xaml.cs
<Window x:Class="ReadXml_1005.MainWindow" 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" xmlns:local="clr-namespace:ReadXml_1005" mc:Ignorable="d" Title="MainWindow" Height="300" Width="500"> <StackPanel Grid.Row="0" Grid.Column="2"> <ListView x:Name="listViewStudents2" Margin="5"> <ListView.View> <GridView> <GridViewColumn Header="Id" Width="80" DisplayMemberBinding="{Binding XPath=@Id}"></GridViewColumn> <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding XPath=Name}"></GridViewColumn> <GridViewColumn Header="Age" Width="120" DisplayMemberBinding="{Binding XPath=Age}"></GridViewColumn> </GridView> </ListView.View> </ListView> <Button Content="Load" Click="Button_Click4" Height="25" Margin="5,0"></Button> </StackPanel> </Window>MainWindow.xaml
<?xml version="1.0" encoding="utf-8" ?> <StudentList> <Student Id = "1"> <Name>Tim</Name> <Age>28</Age> </Student> <Student Id = "2"> <Name>Tom</Name> <Age>29</Age> </Student> <Student Id = "2"> <Name>Vina</Name> <Age>30</Age> </Student> <Student Id = "3"> <Name>Emily</Name> <Age>31</Age> </Student> </StudentList>student.xml