前台xaml页面 <Window x:Class="WpfApplication6.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" xmlns:editors="http://schemas.actiprosoftware.com/winfx/xaml/editors" xmlns:my="clr-namespace:System;assembly=System"> <Grid Name="grid"> <Grid.Resources> <XmlDataProvider x:Key="Userdata" Source="F:/新建文件夹/WpfApplication6/WpfApplication6/bin/Debug/User.xml" /><!--指定该xml文件的地址--> </Grid.Resources> <Button Name="btnNewxml" Margin="40,0,383,291" Height="20" Width="80" Content="创建xml文档" /> <Button Name="btnReadxml" Margin="40,28,383,263" Height="20" Width="80" Content="读取xml文档" /> <Button x:Name="btnInsert" Margin="40,54,383,238" Height="20" Width="80" Content="插入节点" /> <Button Name="btnUpdatexml" Margin="40,80,383,212" Height="20" Width="80" Content="修改节点" /> <Button Name="btnDeletexml" Margin="40,106,383,184" Height="20" Width="80" Content="删除节点" /> <ListView Height="157" HorizontalAlignment="Left" Margin="40,0,0,20" Name="listXml" VerticalAlignment="Bottom" Width="451" DataContext="{Binding Source={StaticResource Userdata},XPath=User}" ItemsSource="{Binding}"> <!--listview的绑定--> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header="用户名" DisplayMemberBinding="{Binding XPath=Name}"></GridViewColumn> <GridViewColumn Header="年龄" DisplayMemberBinding="{Binding XPath=Age}" ></GridViewColumn> </GridView.Columns> </GridView> </ListView.View> </ListView> <TextBox Height="23" HorizontalAlignment="Left" Margin="371,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /> </Grid> </Window> 后台xml文件的操作 using System; using System.Collections.Generic; using System.Linq; using System.Text; 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; using System.Xml.Linq; using System.Xml.XPath; using System.IO; namespace WpfApplication6 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.btnNewxml.Click += new RoutedEventHandler(btnNewxml_Click); this.btnReadxml.Click += new RoutedEventHandler(btnReadxml_Click); this.btnInsert.Click += new RoutedEventHandler(btnInsert_Click); this.btnUpdatexml.Click += new RoutedEventHandler(btnUpdatexml_Click); this.btnDeletexml.Click += new RoutedEventHandler(btnDeletexml_Click); } void btnDeletexml_Click(object sender, RoutedEventArgs e)//删除xml文件的中节点 { XmlDocument xml = new XmlDocument(); xml.Load("User.xml"); XmlNodeList xm = xml.SelectSingleNode("User").ChildNodes; { foreach (XmlNode xl in xm) { if (xl.Name == "Name" && xl.InnerText == "11111") { xl.RemoveAll(); MessageBox.Show("删除成功!"); } } xml.Save("User.xml"); } } void btnUpdatexml_Click(object sender, RoutedEventArgs e)//更新xml文件中的节点 { XmlDocument xml = new XmlDocument(); xml.Load("User.xml"); XmlNodeList nodelist=xml.SelectSingleNode("User").ChildNodes;//搜索所有节点 foreach(XmlNode xm in nodelist) { if (xm.Name == "Name") { if (xm.InnerText == "xiaoqing") { xm.InnerText = "11111"; MessageBox.Show("修改成功!"); break; } } } xml.Save("User.xml"); } void btnInsert_Click(object sender, RoutedEventArgs e)//插入节点 { XmlDocument xml = new XmlDocument(); xml.Load(@"User.xml"); XmlNode root=xml.SelectSingleNode("User"); XmlElement element = xml.CreateElement("Name"); element.InnerText = "小红"; root.AppendChild(element); XmlElement element2 = xml.CreateElement("Age"); element2.InnerText = "20"; root.AppendChild(element2); xml.Save("User.xml"); } void btnReadxml_Click(object sender, RoutedEventArgs e)//读取xml文件中的节点 { XmlDocument doc = new XmlDocument(); doc.Load(@"User.xml"); XmlNode xml = doc.SelectSingleNode("User"); XmlNodeList list = xml.ChildNodes; string[] s = new string[10]; foreach (XmlNode xm in list) { XmlElement element = (XmlElement)xm; for (int i = 1; i < xml.ChildNodes.Count; i++) { s[i]= element.InnerText; } } } void btnNewxml_Click(object sender, RoutedEventArgs e)//新建xml文件中的节点 { XmlDocument xml = new XmlDocument(); xml.LoadXml ( "<User>"+ "<Name>lucy</Name>"+ "<Age>18</Age>"+ "</User>" ); xml.Save("User.xml"); string[] str = Directory.GetFiles(@"F:/新建文件夹/WpfApplication6/WpfApplication6/bin/Debug", "*.xml");//判断xml文件是否新建成功 if (str.Length != 0) { MessageBox.Show("创建成功!"); } else { MessageBox.Show("创建失败!"); } } } }