码迷,mamicode.com
首页 > Windows程序 > 详细

WPF系列

时间:2015-09-10 17:27:04      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

一、ListView绑定数据源XML

 //前端代码
1
<Window x:Class="ListView读取XML数据.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" 3 Height="350" Width="525"> 4 <Window.Resources> 5 <XmlDataProvider x:Key="Student" Source="Book.xml"> 6 </XmlDataProvider> 7 </Window.Resources> 8 <Grid> 9 <ListView ItemsSource="{Binding Source={StaticResource Student},XPath=/Person/Information}"> 10 </ListView> 11 </Grid> 12 </Window>
 //XML文件
1
<?xml version="1.0" encoding="utf-8" ?> 2 <Person> 3 <Information> 4 <Name>张三</Name> 5 <Age>22</Age> 6 <Sex></Sex> 7 </Information> 8 <Information> 9 <Name>李四</Name> 10 <Age>25</Age> 11 <Sex></Sex> 12 </Information> 13 </Person>
//效果截图
技术分享

 二、保存数据到XML

 1             XmlDocument xml = new XmlDocument();
 2             string str_path = @"J:\WPF\test\XMLData.xml";
 3             XmlNode root = null; 
 4             if (File.Exists(str_path))
 5             {
 6                 xml.Load(str_path);
 7                 root = xml.SelectSingleNode("Person");
 8             }
 9             
10             XmlNodeList nodelist = xml.SelectNodes("/Person/Student/Infor");
11             XmlElement inf,adr, stu, info, UN, IP, DB;
12             bool bl_exist = true;
13             foreach (XmlNode node in nodelist)
14             {
15                 if (node.ChildNodes[1].InnerText == (ServerName.Text + ".").ToString() &&
16                     node.ChildNodes[0].InnerText == (UserName.Text + ".").ToString() && 
17                     node.ChildNodes[2].InnerText ==DataName.Text)
18                 {
19                     bl_exist = false;
20                     break;
21                 }
22             }
23             if (bl_exist)
24             {
25                 stu = xml.CreateElement("Student"); //创建元素
26                 info = xml.CreateElement("Infor");
27                 UN = xml.CreateElement("UserName");
28                 IP = xml.CreateElement("IP");
29                 DB = xml.CreateElement("DataBaseName");
30 
31                 inf = xml.CreateElement("Information");
32                 adr = xml.CreateElement("Address");
33                 adr.InnerText = (UserName.Text + "." + ServerName.Text + "." + DataName.Text).ToString();
34                 root.AppendChild(inf);
35                 inf.AppendChild(adr);
36 
37                 UN.InnerText = (UserName.Text + ".").ToString();
38                 IP.InnerText = (ServerName.Text + ".").ToString();
39                 DB.InnerText = DataName.Text;
40                 stu.AppendChild(info);
41                 info.AppendChild(UN);
42                 info.AppendChild(IP);
43                 info.AppendChild(DB);
44                 root.AppendChild(stu);
45                 MessageBox.Show("数据保存成功");
46             }
47             xml.Save(str_path);   

 

WPF系列

标签:

原文地址:http://www.cnblogs.com/772933011qq/p/4798504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!