xml基本操作和保存配置文件应用实例
引言:在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番。本文首先介绍了xml的基本操作,后面写了一个经常用到的xml保存配置文件的实例。 xml常用方法: 定义xml文档:XmlDocument xmlDoc = new XmlDocument(); 初始化xml文档:xmlDoc.Load("D:book.xml");//找到xml文件 创建根元素:XmlElement xmlElement = xmlDoc.CreateElement("","Employees",""); 创建节点:XmlElement xeSub1 = xmlDoc.CreateElement("title"); 查找Employees节点:XmlNode root = xmlDoc.SelectSingleNode("Employees"); 添加节点:xe1.AppendChild(xeSub1); 更改节点的属性:xe.SetAttribute("Name","李明明"); 移除xe的ID属性:xe.RemoveAttribute("ID"); 删除节点title:xe.RemoveChild(xe2);
1 创建xml文档 因为比较简单,直接写方法及结果。 public void CreateXMLDocument() //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?> //加入Employees根元素 //添加节点 //添加子节点
结果: <?xml version="1.0" encoding="GB2312"?> <Node ISB="2-3631-4" Name="李明"> <title>学习VS</title>- <price> <weight>20</weight> </price> </Node> </Employees> 2 增加节点 XmlDocument xmlDoc = new XmlDocument(); 结果: <?xml version="1.0" encoding="GB2312"?> -<Node ISB="2-3631-4" Name="李明"> <title>学习VS</title>- <price> <weight>20</weight> </price> </Node>- <Node2 Name="张三"> <title>心情好</title> </Node2>- <Node2 Name="张三"> <title>心情好</title> </Node2> </Employees> 3 修改节点: public void ModifyNode() { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load("D:book.xml"); XmlNodeList nodeList = xmlDocument.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历 { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("Name") == "李明") { xe.SetAttribute("Name","李明明");//更改节点的属性 XmlNodeList xnl = xe.ChildNodes;//获取xe的所有子节点 foreach (XmlNode xn1 in xnl) { XmlElement xe2 = (XmlElement)xn1;//将节点xn1的属性转换为XmlElement if (xe2.Name == "title")//找到节点名字为title的节点 { xe2.InnerText = "今天天气不好"; } if (xe2.Name == "price") { XmlNodeList xnl2 = xe2.ChildNodes; foreach (XmlNode xn2 in xnl2) { if (xn2.Name == "weight") { xn2.InnerText = "88"; } } } } } } xmlDocument.Save("D:book2.xml"); } result: <?xml version="1.0" encoding="GB2312"?> -<Employees> -<Node ISB="2-3631-4" Name="李明明"> <title>今天天气不好</title>-<price> <weight>88</weight> </price> </Node> -<Node2 Name="张三"> <title>心情好</title> </Node2></Employees>
public void DeleteNode() { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load("D:book1.xml"); XmlNodeList xnl = xmlDocument.SelectSingleNode("Employees").ChildNodes; foreach (XmlNode xn in xnl) { if (xn.Name == "Node") { XmlElement xe = (XmlElement)xn;//将xn的属性转换为XmlElement xe.RemoveAttribute("ID");//移除xe的ID属性 XmlNodeList xnl2 = xe.ChildNodes; for (int i = 0; i < xnl2.Count; i++) { XmlElement xe2 = (XmlElement)xnl2.Item(i); if (xe2.Name == "title") { xe.RemoveChild(xe2);//删除节点title } } } } xmlDocument.Save("D:book3.xml"); } 结果: <?xml version="1.0" encoding="GB2312"?> -<Employees> -<Node ISB="2-3631-4" Name="李明">-<price> <weight>20</weight> </price> </Node>- <Node2 Name="张三"> <title>心情好</title> </Node2>- <Node2 Name="张三"> <title>心情好</title> </Node2> </Employees>
举例说明: 首先在项目文件中创建一个xml文档: <?xml version="1.0" encoding="utf-8" ?> 在保存配置文件时,最主要使用了两个方法:Load和Save。 Load:初始化xml文档,以便项目文件获取具体的xml节点的值。 public void Load(string path) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(path); XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes; foreach (XmlNode xn in xnl) { if (xn.Name == configuration_ServerAddress) { ServerAddress = xn.InnerText; } } } catch(Exception ex) { } }
Save:在项目系统中进行修改配置文件值后,需要对xml进行重新保存 public void Save(string path) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(path); XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes; foreach (XmlNode xn in xnl) { if (xn.Name == configuration_ServerAddress) { xn.InnerText = ServerAddress; } } xmlDocument.Save(path); } catch (Exception ex) { } }
class ConfigurationManager:INotifyPropertyChanged { public const string managerNode = "configurationN";//根节点 public const string configuration_ServerAddress = "ServerAddress";//子节点 private string _ServerAddress; public string ServerAddress { get { return _ServerAddress; } set { _ServerAddress = value; NotifyPropertyChanged("ServerAddress"); } } public void Load(string path) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(path); XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes; foreach (XmlNode xn in xnl) { if (xn.Name == configuration_ServerAddress) { ServerAddress = xn.InnerText; } } } catch(Exception ex) { } } public void Save(string path) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(path); XmlNodeList xnl = xmlDocument.SelectSingleNode(managerNode).ChildNodes; foreach (XmlNode xn in xnl) { if (xn.Name == configuration_ServerAddress) { xn.InnerText = ServerAddress; } } xmlDocument.Save(path); } catch (Exception ex) { } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } public static ConfigurationManager Instance = new ConfigurationManager(); } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Start(); this.tb1.Text = ConfigurationManager.Instance.ServerAddress.ToString(); } private string path = "CONFIGSystem.xml"; private void button1_Click(object sender,RoutedEventArgs e) { ConfigurationManager.Instance.ServerAddress = this.tb1.Text; ConfigurationManager.Instance.Save(path); } private void button2_Click(object sender,RoutedEventArgs e) { Application.Current.Shutdown(); } private void Start() { ConfigurationManager.Instance.Load(path); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |