C#读写xml文件

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

Code<?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>节点:

CodeXmlDocument 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");

结果为:

Code<?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 xn1 in nls)//遍历         {              XmlElement xe2=(XmlElement)xn1;//转换类型              if(xe2.Name=="author")//如果找到              {                   xe2.InnerText="亚胜";//则修改                   break;//找到退出来就可以了              }         }         break;   }}

xmlDoc.Save("bookstore.xml");//保存。

最后结果为:

Code<?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">节点。

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

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

最后结果为:

Code<?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);//显示子节点点文本    }}

C#读写xml文件,布布扣,bubuko.com

时间: 2024-08-27 13:35:12

C#读写xml文件的相关文章

使用dom4j 读写xml文件

dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个十分优秀的JavaXML API,具有性能优异.功能强大和极其易使用的特点,同时它也是一个开放源代码的软件. String fileName="D:\\version.xml"; File inputXML=new File(fileName); //使用 SAXReader 解析 XML 文档 version.xml SAXReader saxReader=new SAXReader();

iOS 读写xml文件

//*********写入文件,传入要保存的信息*********//保存xml -(void) saveXml:(NSString *)data { //创建文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager ]; //获取路径 //参数NSDocumentDirectory要获取那种路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentD

JAXB方式读写XML文件

import java.io.File; import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXB; import org.apache.log4j.Logger; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import cn.com.starit.ts.nms.collec

PHP读写XML文件的四种方法

PHP对XML文件进行读写操作的方法一共有四种,分别是:字符串方式直接读写.DOMDocument读写. XMLWrite写和XMLReader读.SimpleXML读写,本文将依次对这四种方法进行介绍. 介绍之前首先对本文例子使用的数据和文件进行说明.本文写XML文件的例子都是从MySQL中读取数据然后 写入到XML文件中,读XML文件的例子都是从XML文件中读取数据后组装成数组的格式,数组中每个元素对应数 据库中的一条记录. MySQL中的数据: XML文件: 1 2 3 4 5 6 7 8

cocos2d-x 读写 xml 文件

cocos2d-x 读写 xml 文件 A product of cheungmine 使用cocos2d-x开发2d游戏确实方便,但是对于一般的小游戏,经常需要的工作是UI布局设计和调整,代码改来改去,真不方便.做游戏开发早晚要有自己的UI模块.不妨称之为cocos2d-layout.cocos2d-layout相当于舞台的布景.布景师根据导演的要求(xml)来生成舞台.布景师在现实生活中当然是人来做,在程序里就是一段程序或代码库.这个没用通用的万能的库可以做这个事情,因为游戏的内容是差别很大

PHP读写XML文件(一)

PHP读写XML文件的方法有四种,从本文开始将连续使用四篇博文来分别介绍这四种方法.本文介绍的是第一种方法: 使用字符串操作的方式来对XML文件进行读写操作. 一.PHP字符串方式写XML文件: 首先介绍PHP使用字符串方式写XML文件.本例将读取数据库中的数据,输出为XML文件. 数据库数据如下: 读取数据并写入XML文件代码: <?php /** * function:使用字符串方式写XML文件 * author:JetWu * date:2016.12.03 **/ $mysqli = m

C#读写xml文件的常用方法

已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <bookshop> <book genre="fantasy" ISBN="2-2312-2"> <title>Oberon Legacy</title> <author>Eva</author> <

ASP.NET读写XML文件

本文中的方式主要是用序列化与反序列化的方式来实现读写xml文件,注意,不是特指的web.config文件,是指自定义的config文件. 下面的是一个xml读写操作类,包含读方法用到了反序列化方式,还有写方式,用到了序列化方式. using System; using System.IO; using System.Web; using System.Xml.Serialization; namespace WX_Tools { /// <summary> /// Xml文件读写类 /// &

C# 读写XML文件最简单方法

C#史上最简单读写xml文件方式,创建控制台应用程序赋值代码,就可以运行,需要改动,请自行调整 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace ConsoleApp1 { class Program { public cons