从网上整理所得

XMLDocument来操作XML比较简单,虽然有时效率不是很高。代码如下

已知有一个XML文件(bookstore.xml)如下:


<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>

1、往<bookstore>节点中插入一个<book>节点:


 XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml"); XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性 XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<book>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml");

//===============================================

结果为:


<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>

2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。


  XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
{
xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红” XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
break;//找到退出来就可以了
}
}
break;
}
} xmlDoc.Save("bookstore.xml");//保存。

//==================================================

最后结果为:


<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>

3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。


XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;

   foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("genre")=="fantasy")
{
xe.RemoveAttribute("genre");//删除genre属性
}
elseif(xe.GetAttribute("genre")=="update李赞红")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("bookstore.xml");

//===========================================
最后结果为:


<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>

4、显示所有数据。


 XmlNode xn=xmlDoc.SelectSingleNode("bookstore");

   XmlNodeList xnl=xn.ChildNodes;

   foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
Console.WriteLine(xe.GetAttribute("ISBN")); XmlNodeList xnf1=xe.ChildNodes;
foreach(XmlNode xn2 in xnf1)
{
Console.WriteLine(xn2.InnerText);//显示子节点点文本
}
}

5、查找某一个节点数据。


    XmlElement root = xmldoc.DocumentElement;

    XmlNode node = root.SelectSingleNode("//bookstore//book[author='"+ 变量+"']");//查找指定节点

本文来自CSDN博客,http://blog.csdn.net/fan6662000/archive/2008/11/14/3302290.aspx

C#中对 XML节点进行添加,删除,查找和删除操作的更多相关文章

  1. 从字符串中获取XML节点数据

    从字符串中获取XML节点数据,前一篇<字符串创建XML文档> http://www.cnblogs.com/insus/p/3298579.html 是储存为一个XML文档.现在,Insu ...

  2. Winform中对自定义xml配置文件进行Xml节点的添加与删除

    场景 Winform中自定义xml配置文件后对节点进行读取与写入: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10053213 ...

  3. STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase

    今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0         这个程序使用了vect ...

  4. Android中 string.xml资源 如何添加参数?

    在android 开发,我们通常会用string.xml资源去设置textview等控件的字符串.而值一般是与程序的运行结果无关的. 但有时需要根据运行的结果来显示到控件中,这时字符串资源就不能写死了 ...

  5. 使用JAVA编写电话薄程序,具备添加,查找,删除等功能

    //该程序需要连接数据库.根据word文档要求所有功能均已实现.//大部分方法基本差不多,//在查询修改的时候能输出 最大ID号 和最小ID号,并且可以对输入的ID号进行判断是否存在(具体方法请查看 ...

  6. redis中对list类型某个元素的查找和删除

    我们的信息都是放到redis的缓存中,结构为list,如果知道特定的值的话,通过LREM  key  count  value这样就可以.对于redis的list结构,获取某个位置的值通过 LINDE ...

  7. 物理机连接虚拟机中的数据库及Windows添加防火墙允许端口详细操作步骤

    公司项目中因为会使用到SQL server数据库,但是自己电脑无论安装2008R2或者2014版本都不成功,我想可能是和之前安装的一些Windows的软件存在冲突. 于是便单独创建了一台虚拟机,在虚拟 ...

  8. JavaScript添加、查找、删除元素的一个实例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. (4.31)sql server中的xml数据操作

    关键词:xml数据转为行列方式显示 常规案例: declare @data xml declare @h int set @data=' <bookstore> <row> & ...

随机推荐

  1. invalid configuration x86_64-unknown-linux-gnu' machine x86_64-unknown' not recognized

    转载自:http://blog.csdn.net/php_boy/article/details/7382998 前两天在装机器软件的时候, 出现了下面的错误, invalid configurati ...

  2. 如何查看USB方式连接Android设备的外接设备信息

    1,USB存储设备(如:U盘,移动硬盘): //USB存储设备 插拔监听与 SD卡插拔监听一致. private USBBroadCastReceiver mBroadcastReceiver; In ...

  3. Maven实战(九)---模块聚合和继承

    类之间有聚合和继承关系,Maven也具备这种设计原则. 那么Maven的pom是怎样进行聚合与继承的呢? 一.什么是聚合?为什么要用聚合? 上一篇博客介绍了模块化的基本知识. 有了模块化,那么我们项目 ...

  4. 转 MySQL中的共享锁与排他锁

    原文链接在MySQL中的行级锁,表级锁,页级锁中介绍过,行级锁是Mysql中锁定粒度最细的一种锁,行级锁能大大减少数据库操作的冲突.行级锁分为共享锁和排他锁两种,本文将详细介绍共享锁及排他锁的概念.使 ...

  5. Latex中定义、定理、引理、证明 设置方法总结

    Latex中定义.定理.引理.证明 设置方法总结 在LaTex中需要有关定理.公理.命题.引理.定义等时,常用如下命令 \newtheorem{定理环境名}{标题}[主计数器名] \newtheore ...

  6. java Web服务实现方案(REST+SOAP+XML-RPC)简述及比较

    目前知道的三种主流的Web服务实现方案为:REST:表象化状态转变 (软件架构风格)SOAP:简单对象访问协议 XML-RPC:远程过程调用协议 下面分别作简单介绍: REST:表征状态转移(Repr ...

  7. C#使用CodeDom动态加载cs文件

    public static object Create(string path) { var provOptions = new Dictionary<string, string>(); ...

  8. [svc][op]ssh交互yes问题解决-expect

    Expect是Unix系统中用来进行自动化控制和测试的软件工具C67默认未安装:使用需要安装: yum install expect -ywhich expect #查看安装路径 核心命令: [roo ...

  9. SVN清除,VS中SVN的错误以及全部替换

    是tortoisesvn吧?右键,设置,已保存数据,认证数据,清除 ======= <<<<<<< .mine ||||||| .r15 >>&g ...

  10. ssh-copy-id 安全地复制公钥到远程服务器上

    [root@NB .ssh]# ssh-copy-id -i id_rsa.pub " -p22 root@150.57.38.226" root@150.57.38.226's ...