C操作xml文件入门_第1页
C操作xml文件入门_第2页
C操作xml文件入门_第3页
C操作xml文件入门_第4页
C操作xml文件入门_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、已知有一个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

2、></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.Set

3、Attribute("genre","李赞红");/设置该节点genre属性xe1.SetAttribute("ISBN","2-3631-4");/设置该节点ISBN属性XmlElement xesub1=xmlDoc.CreateElement("title");xesub1.InnerText="CS从入门到精通"/设置文本节点xe1.AppendChild(xesub1);/添加到<book>节点中XmlElement xesub2=xmlDoc.Crea

4、teElement("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");/=结果为: <

5、;?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="李赞红&qu

6、ot; ISBN="2-3631-4"><title>CS从入门到精通</title><author>候捷</author><price>58.3</price></book></bookstore>  2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。XmlNodeList nodeList=xmlDoc.SelectSingleNode(&qu

7、ot;bookstore").ChildNodes;/获取bookstore节点的所有子节点foreach(XmlNode xn in nodeList)/遍历所有子节点XmlElement xe=(XmlElement)xn;/将子节点类型转换为XmlElement类型if(xe.GetAttribute("genre")="李赞红")/如果genre属性值为“李赞红”xe.SetAttribute("genre","update李赞红");/则修改该属性为“update李赞红”XmlNodeList

8、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");/保存。 /=最后结果为:<?xml version="1.0" encoding="gb231

9、2"?><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&g

10、t;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.Se

11、lectSingleNode("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();/删除该节点的全部内

12、容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.Writ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论