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>的文本修改为“亚胜”。
Code
最后结果为:
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);//显示子节点点文本 }}
转载于:https://www.cnblogs.com/LuckyClover90105/p/4468664.html
C# xml文件读取与修改相关推荐
- saxreader读取服务器xml文件,使用SAXReader从xml文件读取元素
我试图使用SAXReader读取下面的xml内容 我必须读取节点名称"SelogerListController"的子元素. 节点名称"SelogerListContro ...
- javascript读取xml文件读取节点数据的例子
分享下用javascript读取xml文件读取节点数据方法. 读取的节点数据,还有一种情况是读取节点属性数据. <head> <title></title> < ...
- 从XML文件读取数据绑定到列表控件2
ComponentArt.Web.UI控件绑定所用XML,同时用于DropDownList的绑定,XML如下: <SiteMap> <item Text="标题一&qu ...
- jdom 读取xml_JDOM分析器–将XML文件读取为Java对象
jdom 读取xml JDOM parser provides us a great Java XML API to read, edit and write XML documents easily ...
- [QT操作XML]QT读写XML文件,QT修改XML文件
[QT操作XML]QT读写XML文件 XML简介 QT操作XML,写入.读取.修改 XML效果演示 XML简介 概念:Extensible Markup Language 可扩展标记语言(可扩展:标签 ...
- java操作XML文件--读取内容
先把问题贴出来:编写一个可以解析xml及修改xml内容的工具类 由于我以前做过Android应用程序开发,之前也解析过xml文件,所以,这道题不是很难,这篇文章我先解决第一个问 ...
- C# xml文件的创建,修改和添加节点 。
最近在做一个项目,设计到xml文件的传输,所以就研究了一下. ,.NET Framework完全支持XML DOM模式,但它不支持SAX模式..NET Framework支持两种不同的分析模式:XML ...
- Springboot 项目中 xml文件读取yml 配置文件
2019独角兽企业重金招聘Python工程师标准>>> 在xml文件中读取yml文件即可,代码如下: 现在spring-boot提倡零配置,但是的如果要集成老的spring的项目,涉 ...
- XML文件读取工具类
/// <summary> /// Author: jiangxiaoqiang /// </summary> public class XmlReader {//====== ...
最新文章
- Blender写实产品创作学习教程
- Android中的JSON详细总结
- 为什么不推荐使用 stop、suspend 方法中断线程?
- 图像处理(一)——使用matlab放缩图像
- 走进移动支付:开启物联网时代的商务之门
- 炫界 (667) -(回应骑两小)_为什么那么多人喜欢骑地平线
- 用户与硬件之间的接口
- 拖延的本质是逃避!| 今日最佳
- 大数学家陶哲轩谈时间管理与高效工作的方法
- ireport怎么生成jasper文件
- 谷歌 AI 中国中心彻底变天了!
- Ubantu16.04LTS麒麟版:取消登录界面的客人回话
- 【Amaple教程】4. 组件
- java实现微信支付之扫码支付
- 人肉搜索将被禁止,大家要保护好自己的个人信息!
- DreamweaverCS6 破解补丁和说明
- Jeston NX ubuntu 搜狗拼音输入法安装
- 创业商业计划PPT模板
- kubernetes(centos7)域名解析失败
- STM32 I2C驱动0.96寸OLED屏
热门文章
- SQL DISTINCT 多字段查询用法
- SRE(Simple Rule Engine) Document
- Vue和iview-admin搭建的项目进行兼容
- 钩子函数和回调函数的区别
- ES6-14 Unicode表示法、字符串方法、模板字符串
- 关于jQuery对象(类数组对象)以及DOM对象相互转化问题——[object Object]和[object HTMLInputElement]
- 多元时代个人信息更需强有力保护
- SpringBoot的配置项
- Android多种View动画:EasyAndroidAnimations
- [Android] 输入系统(二)