C# xml增删查改
C# XML XmlDocument
添加命名空间:
using System.Xml;
定义公共对象:
XmlDocument xmldoc ;
XmlNode xmlnode ;
XmlElement xmlelem ;
1,创建到服务器同名目录下的xml文件:
方法一:

xmldoc = new XmlDocument ( ) ;
//加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0","gb2312",null);
xmldoc.AppendChild (xmldecl); //加入一个根元素
xmlelem = xmldoc.CreateElement ( "" , "Employees" , "" ) ;
xmldoc.AppendChild (xmlelem) ;
//加入另外一个元素
for(int i=1;i<3;i++)
{ XmlNode root=xmldoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=xmldoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre","DouCube");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性 XmlElement xesub1=xmldoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
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);//添加到<Employees>节点中
}
//保存创建好的XML文档
xmldoc.Save ( Server.MapPath("data.xml") ) ;

//////////////////////////////////////////////////////////////////////////////////////
结果:在同名目录下生成了名为data.xml的文件,内容如下,

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>

方法二:

XmlTextWriter xmlWriter;
string strFilename = Server.MapPath("data1.xml") ; xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);//创建一个xml文档
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Employees"); xmlWriter.WriteStartElement("Node");
xmlWriter.WriteAttributeString("genre","DouCube");
xmlWriter.WriteAttributeString("ISBN","2-3631-4"); xmlWriter.WriteStartElement("title");
xmlWriter.WriteString("CS从入门到精通");
xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("author");
xmlWriter.WriteString("候捷");
xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("price");
xmlWriter.WriteString("58.3");
xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.Close();

//////////////////////////////////////////////////////////////////////////////////////
结果:

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>

2,添加一个结点:

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("data.xml"));
XmlNode root=xmlDoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=xmlDoc.CreateElement("Node");//创建一个<Node>节点
xe1.SetAttribute("genre","张三");//设置该节点genre属性
xe1.SetAttribute("ISBN","1-1111-1");//设置该节点ISBN属性 XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="C#入门帮助";//设置文本节点
xe1.AppendChild(xesub1);//添加到<Node>节点中
XmlElement xesub2=xmlDoc.CreateElement("author");
xesub2.InnerText="高手";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("price");
xesub3.InnerText="158.3";
xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<Employees>节点中
xmlDoc.Save ( Server.MapPath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////
结果:在xml原有的内容里添加了一个结点,内容如下,

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
</Employees>

3,修改结点的值(属性和子结点):

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") ); XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点 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 xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亚胜";//则修改
}
}
}
}
xmlDoc.Save( Server.MapPath("data.xml") );//保存。

//////////////////////////////////////////////////////////////////////////////////////
结果:将原来的所有结点的信息都修改了,xml的内容如下,

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="update张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
</Node>
</Employees>

4,修改结点(添加结点的属性和添加结点的自结点):

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") ); XmlNodeList nodeList=xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点 foreach(XmlNode xn in nodeList)
{
XmlElement xe=(XmlElement)xn;
xe.SetAttribute("test","111111"); XmlElement xesub=xmlDoc.CreateElement("flag");
xesub.InnerText="1";
xe.AppendChild(xesub);
}
xmlDoc.Save( Server.MapPath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////
结果:每个结点的属性都添加了一个,子结点也添加了一个,内容如下,

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="DouCube" ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
<flag>1</flag>
</Node>
<Node genre="DouCube" ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
<flag>1</flag>
</Node>
<Node genre="update张三" ISBN="1-1111-1" test="111111">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
<flag>1</flag>
</Node>
</Employees>

5,删除结点中的某一个属性:

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") );
XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
xe.RemoveAttribute("genre");//删除genre属性 XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="flag")//如果找到
{
xe.RemoveChild(xe2);//则删除
}
}
}
xmlDoc.Save( Server.MapPath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////]
结果:删除了结点的一个属性和结点的一个子结点,内容如下,

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node ISBN="2-3631-4" test="111111">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node ISBN="1-1111-1" test="111111">
<title>C#入门帮助</title>
<author>亚胜</author>
<price>158.3</price>
</Node>
</Employees>

6,删除结点:

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load( Server.MapPath("data.xml") );
XmlNode root=xmlDoc.SelectSingleNode("Employees");
XmlNodeList xnl=xmlDoc.SelectSingleNode("Employees").ChildNodes;
for(int i=0;i<xnl.Count;i++)
{
XmlElement xe=(XmlElement)xnl.Item(i);
if(xe.GetAttribute("genre")=="张三")
{
root.RemoveChild(xe);
if(i<xnl.Count)i=i-1;
}
}
xmlDoc.Save( Server.MapPath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////]
结果:删除了符合条件的所有结点,原来的内容:

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
<Node genre="张三" ISBN="1-1111-1">
<title>C#入门帮助</title>
<author>高手</author>
<price>158.3</price>
</Node>
</Employees>

删除后的内容:

<?xml version="1.0" encoding="gb2312"?>
<Employees>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
<Node genre="DouCube" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</Node>
</Employees>

7,按照文本文件读取xml

System.IO.StreamReader myFile = new System.IO.StreamReader(Server.MapPath("data.xml"),System.Text.Encoding.Default);
//注意System.Text.Encoding.Default string myString = myFile.ReadToEnd();//myString是读出的字符串
myFile.Close();
C# xml增删查改的更多相关文章
- XML 增删查改
<?xml version="1.0" encoding="utf-8"?> <users> <person name=" ...
- linq to xml 增删查改
一.XML基本概述 XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境 ...
- c#中xml增删查改
/// <summary> /// xml转list /// </summary> /// <typeparam name="T">目标对象&l ...
- java:Hibernate框架1(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态,增删查改)
1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件 准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cf ...
- 4.在MVC中使用仓储模式进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...
- hibernate基础增删查改简单实例
hibernate 基础理论知识网上很多,可以百度和google.这里不做多的介绍,以一个User表来开展例子 建一个web-project 我这里用了junit单元测试环境来进行增删查改的测试,别的 ...
- SSH框架的多表查询和增删查改 (方法一)中
原创作品,允许转载,转载时请务必标明作者信息和声明本文章==>http://www.cnblogs.com/zhu520/p/7774144.html 这边文章是接的刚刚前一遍的基础上敲的 ...
- SSH框架的多表查询(方法二)增删查改
必须声明本文章==>http://www.cnblogs.com/zhu520/p/7773133.html 一:在前一个方法(http://www.cnblogs.com/zhu520/p ...
- Nhibernate入门篇连接Sqlserver的增删查改
第一步:创建数据库 create table Emp( EmpId int primary key identity, EmpName ), EmpDate date ) 第二步:去官网下载:http ...
随机推荐
- 七、vue中v-for有时候对页面不会重新渲染,数组变化后如何到渲染页面
v-for不能进行双向数据绑定,页面渲染完成后,再次更改v-for遍历的数据,js里面打印的数据看到数据值已经更改,但是页面的数据就是没有渲染,这是为什么呢? vue中v-for和angularj ...
- 关于发布中报“未能加载文件或程序集“Newtonsoft.Json”或它的某一个依赖项”的问题解决方法
遇到这个问题了,我也是醉了,开发就一个还在忙别的事情,我想想自己解决 你们遇到过吗?我在网上找到好多解决的方法,比如改webconfig文件,或者改package.config文件,都没用.但是我看到 ...
- vue-router的使用
关于vue-router的基本使用方法 首先,需要下载vue-router npm install vue-router --save vue-router在html或组件中的展现 ``` &l ...
- Linux操作系统-命令-aptitude install unzip
如果linux系统没有自带unzip,请执行aptitude install unzip以安装. 使用到这条Linux命令的场景是: 当我把Jmeter的压缩包(xxx.zip)拷贝到远程的Linux ...
- Win7 IE11无法打开的可能解决办法
IE11点击后无反应,应该如何解决呢?我的机器出现上述情况后,采用下面的方法解决了问题:第一步,进入Windows 7的运行,打开Regedit,即注册表编辑器.依次找到 >>HKEY_C ...
- PHP魔术方法
魔术方法:两个下划线开头的格式. PHP中的魔术方法总结 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, ...
- 分页(将数据库中的多条数据一页一页的显示在jsp页面中)
一.显示数据库中的多条数据为什么要用分页 在真正的开发中,数据库中所存储的数据绝对不像我们平时所写的那样,仅仅有几条数据,而是有几十条甚至上百条,像淘宝京东的用户把都是上几十万甚至百万的.如果这时候在 ...
- install redis
1.Office Download redis.tar.gz from website --- redis.io 2.uncompact file $ tar zxvf redis-3.2.9.ta ...
- Java Swing项目专栏之项目业务流程与业务逻辑
Java Swing项目专栏 项目前言 这个超市管理项目是从八月初开始的,原以为像我这样的小菜比是完全掌控不了这样的项目的.原因是因为大一大二还是没怎么好好学自己的专业课,这次项目做完,我给自己建立了 ...
- linux source命令与export命令的区别
shell与export命令 用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令或声明变量,也可以创建并运行shell脚本程序.运行shell脚本程序 ...