XML文件的一些操作
Posted likehc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML文件的一些操作相关的知识,希望对你有一定的参考价值。
XML 是被设计用来传输和存储数据的,
XML 必须含有且仅有一个 根节点元素(没有根节点会报错)
源码下载 http://pan.baidu.com/s/1ge2lpM7
好了,我们 先看一个 XML 文件(Cartoon.xml)
<?xml version="1.0" encoding="utf-8"?> <Cartoon> <Character Company="DC"> <Name>超人</Name> <Age>28</Age> </Character> <Character Company="Marve"> <Name>雷神</Name> <Age>30</Age> </Character> </Cartoon>
第二个 XML 文件 (Attribute.xml)
<?xml version="1.0" encoding="utf-8"?> <Cartoon> <Character Name="超人" Company="DC" Age="28" /> <Character Name="雷神" Company="Marve" Age="30" /> </Cartoon>
既然是小型存储文件,就避免不了增 删 改 查.
全部 方法代码 如下 (用了两种方法 增 删 改 查).
using System; using System.IO; using System.Linq; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; namespace XML_Demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //创建XML private void btn_Create_XML_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); XmlDeclaration describe = doc.CreateXmlDeclaration("1.0", "utf-8", null); // 创建 描述信息 doc.AppendChild(describe); // 写入描述 XmlElement cartoon = doc.CreateElement("Cartoon"); //创建根节点 doc.AppendChild(cartoon); // 写入根节点 //子节点1 XmlElement character1 = doc.CreateElement("Character"); character1.SetAttribute("Company", "DC"); XmlElement name1 = doc.CreateElement("Name"); name1.InnerText = "超人"; character1.AppendChild(name1); XmlElement age1 = doc.CreateElement("Age"); age1.InnerText = "28"; character1.AppendChild(age1); cartoon.AppendChild(character1); //子节点写入根节点 //子节点2 XmlElement character2 = doc.CreateElement("Character"); character2.SetAttribute("Company", "Marve"); XmlElement name2 = doc.CreateElement("Name"); name2.InnerText = "雷神"; character2.AppendChild(name2); XmlElement age2 = doc.CreateElement("Age"); age2.InnerText = "30"; character2.AppendChild(age2); cartoon.AppendChild(character2); //子节点写入根节点 doc.Save(@"C:\\Cartoon.xml"); Console.WriteLine("写入成功!"); } //查询XML private void btn_Search_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load(@"C:\\Cartoon.xml"); //获取根节点 XmlElement superHeroes = doc.DocumentElement; // 获取子节点的集合 XmlNodeList heroes = superHeroes.ChildNodes; foreach (XmlNode hero in heroes) { Console.WriteLine(hero.SelectSingleNode("Name").InnerText + ":" + hero.SelectSingleNode("Age").InnerText); } } //修改XML private void btn_Edit_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); var file = @"C:\\Cartoon.xml"; if (File.Exists(file)) { doc.Load(file); //获取根节点 XmlNode root = doc.DocumentElement; //获取节点的所有子节点 XmlNodeList characters = root.ChildNodes; foreach (XmlNode hero in characters) { string node = hero.SelectSingleNode("Name").InnerText; if (node == "超人") { XmlNode age = hero.SelectSingleNode("Age"); age.InnerText = "99"; doc.Save(file); Console.WriteLine("修改成功!"); } } } else { Console.WriteLine("文件不存在!"); } } //修改XML(追加) private void btn_Edit_Add_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); var file = @"C:\\Cartoon.xml"; if (File.Exists(file)) { doc.Load(file); //获取根节点 XmlElement cartoonNew = doc.DocumentElement; XmlElement characterNew = doc.CreateElement("Character"); characterNew.SetAttribute("Company", "DC"); XmlElement nameNew = doc.CreateElement("Name"); nameNew.InnerText = "Doctor Manhattan"; characterNew.AppendChild(nameNew); XmlElement ageNew = doc.CreateElement("Age"); ageNew.InnerText = "24"; characterNew.AppendChild(ageNew); cartoonNew.AppendChild(characterNew); //子节点写入根节点 doc.Save(file); Console.WriteLine("添加成功!"); } else { Console.WriteLine("文件不存在!"); } } //删除XML private void btn_Del_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); var file = @"C:\\Cartoon.xml"; doc.Load(file); XmlNode root = doc.SelectSingleNode("Cartoon"); //root.RemoveAll(); //删除根节点下的 所有节点 foreach (XmlNode xn in root.ChildNodes) { if (xn.FirstChild.InnerText == "超人") { root.RemoveChild(xn); } } doc.Save(file); Console.WriteLine("删除成功"); } //创建XML2 private void btn_Create_XML2_Click(object sender, EventArgs e) { XElement xel = new XElement("Cartoon", new XElement("Character", new XAttribute("Company", "DC"), new XElement("Name", "超人"), new XElement("Age", "28")), new XElement("Character", new XAttribute("Company", "Marve"), new XElement("Name", "雷神"), new XElement("Age", "30")) ); XDocument xdoc = new XDocument(xel); xdoc.Save(@"c:\\Cartoon2.xml"); } //查询XML2 private void btn_Search2_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load(@"C:\\Cartoon2.xml"); XmlNodeList characterList = doc.SelectNodes("/Character"); foreach (XmlNode item in characterList) { Console.WriteLine(item.SelectSingleNode("Name").InnerText+":"+ item.SelectSingleNode("Age").InnerText); } } //修改XML2 private void btn_Edit2_Click(object sender, EventArgs e) { XElement doc = XElement.Load(@"C:\\Cartoon2.xml"); XElement heroes = (from p in doc.Elements() where p.Element("Name").Value == "超人" select p).FirstOrDefault(); if (heroes != null) { heroes.Element("Age").Value = "99"; doc.Save(@"C:\\Cartoon2.xml"); Console.WriteLine("添加成功!"); } } //删除XML2 private void btn_Del2_Click(object sender, EventArgs e) { XElement doc = XElement.Load(@"C:\\Cartoon2.xml"); XElement xchild = doc.Descendants("Name").Single(p => p.Value.Equals("超人")); XElement nameNode = xchild.Parent; nameNode.Remove(); doc.Save(@"C:\\Cartoon2.xml"); } //创建多属性XML private void btn_XML_Attribute_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); XmlDeclaration describe = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(describe); XmlElement cartoon = doc.CreateElement("Cartoon"); doc.AppendChild(cartoon); XmlElement character1 = doc.CreateElement("Character"); character1.SetAttribute("Name", "超人"); character1.SetAttribute("Company","DC"); character1.SetAttribute("Age", "28"); cartoon.AppendChild(character1); XmlElement character2 = doc.CreateElement("Character"); character2.SetAttribute("Name", "雷神"); character2.SetAttribute("Company", "Marve"); character2.SetAttribute("Age", "30"); cartoon.AppendChild(character2); doc.Save(@"C:\\Attribute.xml"); Console.WriteLine("写入成功!"); } //查询多属性XML private void btn_Search_Attribute_Click(object sender, EventArgs e) { var file = @"C:\\Attribute.xml"; XmlDocument doc = new XmlDocument(); doc.Load(file); XmlNodeList xnl = doc.SelectNodes("/Cartoon/Character"); foreach (XmlNode item in xnl) { Console.WriteLine(item.Attributes["Name"].Value ); } } //修改多属性XML private void btn_Edit_Attribute_Click(object sender, EventArgs e) { var file = @"C:\\Attribute.xml"; XElement doc = XElement.Load(file); XElement heroes = (from p in doc.Elements() where p.Attribute("Name").Value == "超人" select p).FirstOrDefault(); if (heroes != null) { heroes.Attribute("Age").Value = "99"; doc.Save(file); Console.WriteLine("修改成功!"); } } //删除多属性XML private void btn_Del_Attribute_Click(object sender, EventArgs e) { var file = @"C:\\Attribute.xml"; XElement xl = XDocument.Load(file).Root; xl.Elements("Character").Where(el => el.Attribute("Name").Value.Equals("超人")).Remove(); xl.Save(file); Console.WriteLine("修改成功!"); } //删除多属性XML2 private void btn_Del_Attribute2_Click(object sender, EventArgs e) { var file = @"C:\\Attribute.xml"; XmlDocument doc = new XmlDocument(); doc.Load(file); XmlElement root = doc.DocumentElement; XmlNodeList element = doc.SelectNodes("/Cartoon/Character"); foreach (XmlNode item in element) { Console.WriteLine(item.Attributes["Name"].Value); if (item.Attributes["Name"].Value == "超人") { root.RemoveChild(item); } } doc.Save(file); } } }
窗体设计代码 (如果不想拖控件,可以复制过去)
1 namespace XML_Demo 2 { 3 partial class Form1 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要修改 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.btn_Create_XML = new System.Windows.Forms.Button(); 32 this.btn_Create_XML2 = new System.Windows.Forms.Button(); 33 this.btn_Search = new System.Windows.Forms.Button(); 34 this.btn_Edit_Add = new System.Windows.Forms.Button(); 35 this.btn_Edit = new System.Windows.Forms.Button(); 36 this.btn_Del = new System.Windows.Forms.Button(); 37 this.btn_Search2 = new System.Windows.Forms.Button(); 38 this.btn_Edit2 = new System.Windows.Forms.Button(); 39 this.btn_Del2 = new System.Windows.Forms.Button(); 40 this.btn_XML_Attribute = new System.Windows.Forms.Button(); 41 this.btn_Search_Attribute = new System.Windows.Forms.Button(); 42 this.btn_Edit_Attribute = new System.Windows.Forms.Button(); 43 this.btn_Del_Attribute = new System.Windows.Forms.Button(); 44 this.btn_Del_Attribute2 = new System.Windows.Forms.Button(); 45 this.SuspendLayout(); 46 // 47 // btn_Create_XML 48 // 49 this.btn_Create_XML.Location = new System.Drawing.Point(13, 12); 50 this.btn_Create_XML.Name = "btn_Create_XML"; 51 this.btn_Create_XML.Size = new System.Drawing.Size(95, 23); 52 this.btn_Create_XML.TabIndex = 0; 53 this.btn_Create_XML.Text = "创建XML"; 54 this.btn_Create_XML.UseVisualStyleBackColor = true; 55 this.btn_Create_XML.Click += new System.EventHandler(this.btn_Create_XML_Click); 56 // 57 // btn_Create_XML2 58 // 59 this.btn_Create_XML2.Location = new System.Drawing.Point(137, 12); 60 this.btn_Create_XML2.Name = "btn_Create_XML2"; 61 this.btn_Create_XML2.Size = new System.Drawing.Size(75, 23); 62 this.btn_Create_XML2.TabIndex = 3; 63 this.btn_Create_XML2.Text = "创建XML2"; 64 this.btn_Create_XML2.UseVisualStyleBackColor = true; 65 this.btn_Create_XML2.Click += new System.EventHandler(this.btn_Create_XML2_Click); 66 // 67 // btn_Search 68 // 69 this.btn_Search.Location = new System.Drawing.Point(13, 57); 70 this.btn_Search.Name = "btn_Search"; 71 this.btn_Search.Size = new System.Drawing.Size(95, 23); 72 this.btn_Search.TabIndex = 4; 73 this.btn_Search.Text = "查询XML"; 74 this.btn_Search.UseVisualStyleBackColor = true; 75 this.btn_Search.Click += new System.EventHandler(this.btn_Search_Click); 76 // 77 // btn_Edit_Add 78 // 79 this.btn_Edit_Add.Location = new System.Drawing.Point(13, 139); 80 this.btn_Edit_Add.Name = "btn_Edit_Add"; 81 this.btn_Edit_Add.Size = new System.Drawing.Size(95, 23); 82 this.btn_Edit_Add.TabIndex = 5; 83 this.btn_Edit_Add.Text = "修改XML(追加)"; 84 this.btn_Edit_Add.UseVisualStyleBackColor = true; 85 this.btn_Edit_Add.Click += new System.EventHandler(this.btn_Edit_Add_Click); 86 // 87 // btn_Edit 88 // 89 this.btn_Edit.Location = new System.Drawing.Point(13, 100); 90 this.btn_Edit.Name = "第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错从 XML 声明片段获取 XML 编码:部分内容解析不支持 XmlDeclaration
android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错