XML文件是一种经常使用的文件格式,无论是B/S仍是C/S都随处可见XML的身影。Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具。XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记能够用方便的方式创建,虽然XML占用的空间比二进制数据要占用更多的空间,但XML极其简单易于掌握和使用。微软也提供了一系列类库来倒帮助咱们在应用程序中存储XML文件。java
“在程序中访问进而操做XML文件通常有两种模型,分别是使用DOM(文档对象模型)和流模型,使用DOM的好处在于它容许编辑和更新XML文档,能够随机访问文档中的数据,可使用XPath查询,可是,DOM的缺点在于它须要一次性的加载整个文档到内存中,对于大型的文档,这会形成资源问题。流模型很好的解决了这个问题,由于它对XML文件的访问采用的是流的概念,也就是说,任什么时候候在内存中只有当前节点,但它也有它的不足,它是只读的,仅向前的,不能在文档中执行向后导航操做。”node
首先来看一个简单的XML文件:工具
<?xml version="1.0" encoding="UTF-8"?> <Persons> <Person id="1"> <Name>FlyElephant</Name> <Age>24</Age> </Person> <Person id="2"> <Name>keso</Name> <Age>25</Age> </Person> </Persons>
这是最多见的Dom形式的XML文件,建立的话也比较简单,代码以下:spa
XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); doc.AppendChild(dec); //根节点 XmlElement root = doc.CreateElement("Persons"); doc.AppendChild(root); //根节点的添加独立子节点 XmlElement person = doc.CreateElement("Person"); person.SetAttribute("id", "1"); person.AppendChild(getChildNode(doc, "Name", "FlyElephant")); person.AppendChild(getChildNode(doc, "Age", "24")); root.AppendChild(person); //根节点的添加独立子节点 person = doc.CreateElement("Person"); person.SetAttribute("id", "2"); person.AppendChild(getChildNode(doc, "Name", "keso")); person.AppendChild(getChildNode(doc, "Age", "25")); root.AppendChild(person); doc.Save("person.xml"); Console.WriteLine("建立成功");
C#中读取XML有三种方式,XmlDocument,XmlTextReader,Linq to Xml,因为本人经常使用的是XmlDocument方法,其余的方法,有兴趣的能够本身尝试一下,看下查询的实现:xml
XmlDocument doc = new XmlDocument(); doc.Load("person.xml"); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList personNodes = root.GetElementsByTagName("Person"); //获取Person子节点集合 foreach (XmlNode node in personNodes) { string id = ((XmlElement)node).GetAttribute("id"); //获取Name属性值 string name = ((XmlElement)node).GetElementsByTagName("Name")[0].InnerText; //获取Age子XmlElement集合 string age = ((XmlElement)node).GetElementsByTagName("Age")[0].InnerText; Console.WriteLine("编号:" + id + "姓名:" + name + "年龄:" + age); }
结果以下:对象
XML存放的是数据结果跟类类似,若是业务须要往里面动态添加数据,这个时候也须要我的控制一下,代码以下:blog
XmlDocument doc = new XmlDocument(); doc.Load("person.xml"); XmlElement root = doc.DocumentElement; //根节点的添加独立子节点 XmlElement person = doc.CreateElement("Person"); person.SetAttribute("id", "3"); person.AppendChild(getChildNode(doc, "Name", "Elephant")); person.AppendChild(getChildNode(doc, "Age", "23")); root.AppendChild(person); doc.Save("person.xml"); Console.WriteLine("XML文件中节点添加成功");
这个时候XML文件已经发生变化:内存
<?xml version="1.0" encoding="UTF-8"?> <Persons> <Person id="1"> <Name>FlyElephant修改</Name> <Age>24</Age> </Person> <Person id="2"> <Name>keso</Name> <Age>25</Age> </Person> <Person id="3"> <Name>Elephant</Name> <Age>23</Age> </Person> </Persons>
修改其中的一个节点的话,最简单的就是遍历,遍历的时候修改本身想要修改的元素:资源
XmlDocument doc = new XmlDocument(); doc.Load("person.xml"); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList personNodes = root.GetElementsByTagName("Person"); //获取Person子节点集合 foreach (XmlNode node in personNodes) { XmlElement ele = (XmlElement)node; if (ele.GetAttribute("id") == "3") { XmlElement nameEle = (XmlElement)ele.GetElementsByTagName("Name")[0]; nameEle.InnerText = nameEle.InnerText + "修改"; } } Console.WriteLine("节点修改为功"); doc.Save("person.xml");
固然若是XML文件中内容不少的话,这种方式就显得的不是那么的合理,能够修改一下一上代码文档
XmlElement selectEle = (XmlElement)root.SelectSingleNode("/Persons/Person[@id='1']"); XmlElement nameEle = (XmlElement)selectEle.GetElementsByTagName("Name")[0]; nameEle.InnerText = nameEle.InnerText + "修改";
通过上面的操做,删除节点就很简单的,代码以下:
XmlDocument doc = new XmlDocument(); doc.Load("person.xml"); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList personNodes = root.GetElementsByTagName("Person"); //获取Person子节点集合 XmlNode selectNode =root.SelectSingleNode("/Persons/Person[@id='1']"); root.RemoveChild(selectNode); Console.WriteLine("节点删除成功"); doc.Save("person.xml");
周末看博客的都是强人,你们周末愉快~