C#操作XML方法:新增、修改和删除节点与属性
一 前言
先来了解下操作XML所涉及到的几个类及之间的关系 如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家
* 1 XMLElement 主要是针对节点的一些属性进行操作
* 2 XMLDocument 主要是针对节点的CUID操作
* 3 XMLNode 为抽象类,做为以上两类的基类,提供一些操作节点的方法

清楚了以上的关系在操作XML时会更清晰一点
二 具体操作
以下会对Xml的结点与属性做增 删 改 查的操作也满足了实际工作中的大部分情况
先构造一棵XML树如下,其中也涉及到了写入xml文档的操作
1 public void CreatXmlTree(string xmlPath)
2 {
3 XElement xElement = new XElement(
4 new XElement("BookStore",
5 new XElement("Book",
6 new XElement("Name", "C#入门", new XAttribute("BookName", "C#")),
7 new XElement("Author", "Martin", new XAttribute("Name", "Martin")),
8 new XElement("Adress", "上海"),
9 new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
10 ),
11 new XElement("Book",
12 new XElement("Name", "WCF入门", new XAttribute("BookName", "WCF")),
13 new XElement("Author", "Mary", new XAttribute("Name", "Mary")),
14 new XElement("Adress", "北京"),
15 new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
16 )
17 )
18 );
19
20 //需要指定编码格式,否则在读取时会抛:根级别上的数据无效。 第 1 行 位置 1异常
21 XmlWriterSettings settings = new XmlWriterSettings();
22 settings.Encoding = new UTF8Encoding(false);
23 settings.Indent = true;
24 XmlWriter xw = XmlWriter.Create(xmlPath,settings);
25 xElement.Save(xw);
26 //写入文件
27 xw.Flush();
28 xw.Close();
29 }
然后得到如下的XML树
1 <?xml version="1.0" encoding="utf-8"?>
2 <BookStore>
3 <Book>
4 <Name BookName="C#">C#入门</Name>
5 <Author Name="Martin">Martin</Author>
6 <Date>2013-10-11</Date>
7 <Adress>上海</Adress>
8 <Date>2013-10-11</Date>
9 </Book>
10 <Book>
11 <Name BookName="WCF">WCF入门</Name>
12 <Author Name="Mary">Mary</Author>
13 <Adress>北京</Adress>
14 <Date>2013-10-11</Date>
15 </Book>
16 </BookStore>
以下操作都是对生成的XML树进行操作
2.1 新增节点与属性
新增节点NewBook并增加属性Name="WPF"
1 public void Create(string xmlPath)
2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(xmlPath);
5
6 var root = xmlDoc.DocumentElement;//取到根结点
7 XmlNode newNode = xmlDoc.CreateNode("element", "NewBook", "");
8 newNode.InnerText = "WPF";
9
10 //添加为根元素的第一层子结点
11 root.AppendChild(newNode);
12 xmlDoc.Save(xmlPath);
13 }
开篇有写操作xml节点属性主要用XmlElement对象所以取到结点后要转类型
1 //属性
2 public void CreateAttribute(string xmlPath)
3 {
4 XmlDocument xmlDoc = new XmlDocument();
5 xmlDoc.Load(xmlPath);
6 var root = xmlDoc.DocumentElement;//取到根结点
7 XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
8 node.SetAttribute("Name", "WPF");
9 xmlDoc.Save(xmlPath);
10
11 }
效果如下

2.2 删除节点与属性
1 public void Delete(string xmlPath)
2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(xmlPath);
5 var root = xmlDoc.DocumentElement;//取到根结点
6
7 var element = xmlDoc.SelectSingleNode("BookStore/NewBook");
8 root.RemoveChild(element);
9 xmlDoc.Save(xmlPath);
10 }
删除属性
1 public void DeleteAttribute(string xmlPath)
2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(xmlPath);
5 XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6 //移除指定属性
7 node.RemoveAttribute("Name");
8 //移除当前节点所有属性,不包括默认属性
9 //node.RemoveAllAttributes();
10 xmlDoc.Save(xmlPath);
11 }
2.3 修改节点与属性
xml的节点默认是不允许修改的,本文也就不做处理了
修改属性代码如下
1 public void ModifyAttribute(string xmlPath)
2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(xmlPath);
5 XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6 element.SetAttribute("Name", "Zhang");
7 xmlDoc.Save(xmlPath);
8 }
效果如下

2.4 获取节点与属性
1 public void Select(string xmlPath)
2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(xmlPath);
5 //取根结点
6 var root = xmlDoc.DocumentElement;//取到根结点
7 //取指定的单个结点
8 XmlNode oldChild = xmlDoc.SelectSingleNode("BookStore/NewBook");
9
10 //取指定的结点的集合
11 XmlNodeList nodes = xmlDoc.SelectNodes("BookStore/NewBook");
12
13 //取到所有的xml结点
14 XmlNodeList nodelist = xmlDoc.GetElementsByTagName("*");
15 }
属性
1 public void SelectAttribute(string xmlPath)
2 {
3 XmlDocument xmlDoc = new XmlDocument();
4 xmlDoc.Load(xmlPath);
5 XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6 string name = element.GetAttribute("Name");
7 Console.WriteLine(name);
8 }
三 linq to XML 操作
Linq to Xml 也没什么变化只操作对象改变了主要涉及的几个对象如下 注:我并没有用linq的语法去操作元素。
XDocument:用于创建一个XML实例文档
XElement:用于一些节点与节点属性的基本操作
以下是对Xml的 一些简单的操作
3.1 新增节点与属性
1 public void Create(string xmlPath)
2 {
3 XDocument xDoc = XDocument.Load(xmlPath);
4 XElement xElement = xDoc.Element("BookStore");
5 xElement.Add(new XElement("Test", new XAttribute("Name", "Zery")));
6 xDoc.Save(xmlPath);
7 }
属性
1 public void CreateAttribute(string xmlPath)
2 {
3 XDocument xDoc = XDocument.Load(xmlPath);
4 IEnumerable<XElement> xElement = xDoc.Element("BookStore").Elements("Book");
5 foreach (var element in xElement)
6 {
7 element.SetAttributeValue("Name", "Zery");
8 }
9 xDoc.Save(xmlPath);
10 }
3.2 删除节点与属性
1 public void Delete(string xmlPath)
2 {
3 XDocument xDoc = XDocument.Load(xmlPath);
4 XElement element = (XElement)xDoc.Element("BookStore").Element("Book");
5 element.Remove();
6 xDoc.Save(xmlPath);
7 }
属性
1 public void DeleteAttribute(string xmlPath)
2 {
3 XDocument xDoc = XDocument.Load(xmlPath);
4 //不能跨级取节点
5 XElement element = xDoc.Element("BookStore").Element("Book").Element("Name");
6 element.Attribute("BookName").Remove();
7 xDoc.Save(xmlPath);
8 }
3.3 修改节点属性
节点.net没提供修改的方法本文也不做处理
修改属性与新增实质是同一个方法
1 public void ModifyAttribute(string xmlPath)
2 {
3 XDocument xDoc = XDocument.Load(xmlPath);
4 XElement element = xDoc.Element("BookStore").Element("Book");
5 element.SetAttributeValue("BookName","ZeryTest");
6 xDoc.Save(xmlPath);
7 }
四 总结
把文章写完时,又扫去了自己的一个盲区,虽然都是些简单的操作,但在实际的开中,又何尝不是由简单到复杂呢。我觉得身为程序员就应该遇到自己的盲区时,立马花时间去了解,不说要了解多深入,但至少基本的还是要知道,等到工作中真需时,只要稍微花点时间就可以了。
C#操作XML方法:新增、修改和删除节点与属性的更多相关文章
- .net操作xml文件(新增.修改,删除,读取) 转
今天有个需求需要操作xml节点.突然见遗忘了许多.上网看了些资料.才整出来.脑袋真不够用.在这里把我找到的资料共享一下.方便以后使用.本文属于网摘/ 1 一.简单介绍2 using System.Xm ...
- .net操作xml文件(新增.修改,删除,读取)---datagridview与xml文件
参考网址: http://www.cnblogs.com/liguanghui/archive/2011/11/10/2244199.html 很详细的,相信能给你一定的帮助.
- C#操作XML方法集合
一 前言 先来了解下操作XML所涉及到的几个类及之间的关系 如果大家发现少写了一些常用的方法,麻烦在评论中指出,我一定会补上的!谢谢大家 * 1 XMLElement 主要是针对节点的一些属性进行操 ...
- 摘:通过ICursor对Table进行操作(添加、修改、删除)
通过ICursor对Table进行操作(添加.修改.删除) 连接上数据表的目的就是对其进行包括浏览.添加.修改.删除等基本操作. 浏览功能,之前文章中一提到,就是将Itable转换为DataTable ...
- 通过ICursor对Table进行操作(添加、修改、删除)
通过ICursor对Table进行操作(添加.修改.删除) 2010-03-16 16:07:37| 分类: 工作|举报|字号 订阅 来自:http://blog.163.com/liuyang12 ...
- JS 对象API之修改、删除对象的属性
无论是修改还是删除对象的属性,我们首先要清楚:自有属性.共有属性的处理方法肯定是不同的: 先创建一个对象实例 var obj = { name: '小马扎', age: }; Object.proto ...
- C#操作XML方法详解
using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xml.Load(path); xml. ...
- PHP操作XML方法之SimpleXML
SimpleXML简介 SimpleXML 扩展提供了一个非常简单和易于使用的工具集,能将XML转换成一个带有一般属性选择器和数组迭代器的对象. 举例XML XML结构部分引用自<<深入理 ...
- C#.Net操作XML方法二
上面那篇博客,在上面那面博客中是通过System.Xml命名空间中的类来实现对XML文件的创建.删除和改动等操作.接下来再介绍一种方法,在整个的操作过程中,仅仅只是换了个类而已,没什么大惊小怪的. D ...
随机推荐
- Codeforces Gym100814 F.Geometry (ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology)
这个题真的是超级超级水啊,哈哈哈哈哈哈.不要被题面吓到,emnnn,就这样... 代码: 1 #include<iostream> 2 #include<cstring> 3 ...
- HDU 1223 还是畅通过程【最小生成树模板】
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 洛谷——P1462 通往奥格瑞玛的道路
P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...
- Java---杨辉三角简易解法(通俗易懂,逻辑严密)
- Java中Properties配置文件读取
以下实践的是Properties配置文件的基本操作方法.像spring使用xml做依赖注入时,这个配置文件起到非常实用的作用. 一.格式规范 参考wiki百科的格式简介:https://zh.wiki ...
- 开始docker
安装 docker 目前只支持64位系统 1.下载并安装,简直太方便了 $ curl -fsSL https://get.docker.com/ | sh 用例 .docker run hel ...
- CentOS 挂载NTFS分区的两种方法
第一种是安装内核模块,可到 http://sourceforge.net/projects/linux-ntfs/files/ 下载,需下载与你系统内核想对应的版本,使用uname -a 查看当前内核 ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'
原因:找不到mysql.sock文件 解决方法: 1 找到mysql.sock文件位置 echo "show variables" | mysql | grep "soc ...
- Linux下报错:Segmentation fault.
遇到的问题:程序在读文件之后,准备执行fclose(fp);时,出现了如下错误: Program received signal SIGSEGV, Segmentation fault. 解决方法:倒 ...
- angular js 使用$location问题整理
angular js 自带的$location方法十分强大,通过使用$location方法.我们能够获取到server的port.杂乱连接中的path()部分(/所包括的部分). 例: // give ...