C#操作xml文件实例详解

开发 后端
C#操作xml文件实例主要通过一个实例向你讲述了C#操作xml文件的整个过程,希望对你有所帮助。

C#操作xml文件实例是如何的呢?让我们先看看问题:

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

  1. ﹤?xml version="1.0" encoding="gb2312"?﹥  
  2. ﹤bookstore﹥  
  3. ﹤book genre="fantasy" ISBN="2-3631-4"﹥  
  4. ﹤title﹥Oberon's Legacy﹤/title﹥  
  5. ﹤author﹥Corets, Eva﹤/author﹥  
  6. ﹤price﹥5.95﹤/price﹥  
  7. ﹤/book﹥  
  8. ﹤/bookstore﹥ 

C#操作xml文件实例1、

往﹤bookstore﹥节点中插入一个﹤book﹥节点:

  1. XmlDocument xmlDoc=new XmlDocument();  
  2. xmlDoc.Load("bookstore.xml");  
  3. XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找﹤bookstore﹥  
  4. XmlElement xe1=xmlDoc.CreateElement("book");//创建一个﹤book﹥节点  
  5. xe1.SetAttribute("genre","李赞红");//设置该节点genre属性  
  6. xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性  
  7.  
  8. XmlElement xesub1=xmlDoc.CreateElement("title");  
  9. xesub1.InnerText="CS从入门到精通";//设置文本节点  
  10. xe1.AppendChild(xesub1);//添加到﹤book﹥节点中  
  11. XmlElement xesub2=xmlDoc.CreateElement("author");  
  12. xesub2.InnerText="候捷";  
  13. xe1.AppendChild(xesub2);  
  14. XmlElement xesub3=xmlDoc.CreateElement("price");  
  15. xesub3.InnerText="58.3";  
  16. xe1.AppendChild(xesub3);  
  17.  
  18. root.AppendChild(xe1);//添加到﹤bookstore﹥节点中  
  19. xmlDoc.Save("bookstore.xml");  
  20.  
  21. //================  

C#操作xml文件实例结果为:

  1. ﹤?xml version="1.0" encoding="gb2312"?﹥  
  2. ﹤bookstore﹥  
  3. ﹤book genre="fantasy" ISBN="2-3631-4"﹥  
  4. ﹤title﹥Oberon's Legacy﹤/title﹥  
  5. ﹤author﹥Corets, Eva﹤/author﹥  
  6. ﹤price﹥5.95﹤/price﹥  
  7. ﹤/book﹥  
  8. ﹤book genre="李赞红" ISBN="2-3631-4"﹥  
  9. ﹤title﹥CS从入门到精通﹤/title﹥  
  10. ﹤author﹥候捷﹤/author﹥  
  11. ﹤price﹥58.3﹤/price﹥  
  12. ﹤/book﹥  
  13. ﹤/bookstore﹥ 

C#操作xml文件实例2、

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

  1. XmlNodeList nodeList=xmlDoc.  
  2. SelectSingleNode("bookstore").ChildNodes;  
  3. //获取bookstore节点的所有子节点  
  4. foreach(XmlNode xn in nodeList)  
  5. //遍历所有子节点  
  6. {  
  7. XmlElement xe=(XmlElement)xn;  
  8. //将子节点类型转换为XmlElement类型  
  9. if(xe.GetAttribute("genre")=="李赞红")  
  10. //如果genre属性值为“李赞红”  
  11. {  
  12. xe.SetAttribute("genre","update李赞红");  
  13. //则修改该属性为“update李赞红”  
  14.  
  15. XmlNodeList nls=xe.ChildNodes;  
  16. //继续获取xe子节点的所有子节点  
  17. foreach(XmlNode xn1 in nls)//遍历  
  18. {  
  19. XmlElement xe2=(XmlElement)xn1;  
  20. //转换类型  
  21. if(xe2.Name=="author")//如果找到  
  22. {  
  23. xe2.InnerText="亚胜";//则修改  
  24. break;//找到退出来就可以了  
  25. }  
  26. }  
  27. break;  
  28. }  
  29. }  
  30.  
  31. xmlDoc.Save("bookstore.xml");//保存。  
  32.  
  33. //=================  

C#操作xml文件实例***结果为:

  1. ﹤?xml version="1.0" encoding="gb2312"?﹥  
  2. ﹤bookstore﹥  
  3. ﹤book genre="fantasy" ISBN="2-3631-4"﹥  
  4. ﹤title﹥Oberon's Legacy﹤/title﹥  
  5. ﹤author﹥Corets, Eva﹤/author﹥  
  6. ﹤price﹥5.95﹤/price﹥  
  7. ﹤/book﹥  
  8. ﹤book genre="update李赞红" ISBN="2-3631-4"﹥  
  9. ﹤title﹥CS从入门到精通﹤/title﹥  
  10. ﹤author﹥亚胜﹤/author﹥  
  11. ﹤price﹥58.3﹤/price﹥  
  12. ﹤/book﹥  
  13. ﹤/bookstore﹥ 

C#操作xml文件实例3、

删除

  1. ﹤book genre="fantasy"   
  2. ISBN="2-3631-4"﹥节点的genre属性,删除   
  3. ﹤book genre="update李赞红" ISBN="2-3631-4"﹥节点。  
  4.  
  5. XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;  
  6.  
  7. foreach(XmlNode xn in xnl)  
  8. {  
  9. XmlElement xe=(XmlElement)xn;  
  10.  
  11. if(xe.GetAttribute("genre")=="fantasy")  
  12. {  
  13. xe.RemoveAttribute("genre");//删除genre属性  
  14. }  
  15. else if(xe.GetAttribute("genre")=="update李赞红")  
  16. {  
  17. xe.RemoveAll();//删除该节点的全部内容  
  18. }  
  19. }  
  20. xmlDoc.Save("bookstore.xml");  
  21.  
  22. //====================  

C#操作xml文件实例***结果为:

  1. ﹤?xml version="1.0" encoding="gb2312"?﹥  
  2. ﹤bookstore﹥  
  3. ﹤book ISBN="2-3631-4"﹥  
  4. ﹤title﹥Oberon's Legacy﹤/title﹥  
  5. ﹤author﹥Corets, Eva﹤/author﹥  
  6. ﹤price﹥5.95﹤/price﹥  
  7. ﹤/book﹥  
  8. ﹤book﹥  
  9. ﹤/book﹥  
  10. ﹤/bookstore﹥  

C#操作xml文件实例4、

显示所有数据。

  1. XmlNode xn=xmlDoc.SelectSingleNode("bookstore");  
  2.  
  3. XmlNodeList xnl=xn.ChildNodes;  
  4.  
  5. foreach(XmlNode xnf in xnl)  
  6. {  
  7. XmlElement xe=(XmlElement)xnf;  
  8. Console.WriteLine(xe.GetAttribute("genre"));//显示属性值  
  9. Console.WriteLine(xe.GetAttribute("ISBN"));  
  10.  
  11. XmlNodeList xnf1=xe.ChildNodes;  
  12. foreach(XmlNode xn2 in xnf1)  
  13. {  
  14. Console.WriteLine(xn2.InnerText);//显示子节点点文本  
  15. }  
  16. }  

C#操作xml文件实例的基本内容就向你介绍到这里,希望对你了解和学习C#操作xml文件有所帮助。

【编辑推荐】

  1. C# 操作Excel之读取Excel操作浅析
  2. C# 操作Excel之写入Excel操作浅析
  3. C# 操作XML之建立Xml对象浅析
  4. C# 操作XML之读取Xml浅析
  5. C# 操作XML的全过程实例浅析
责任编辑:仲衡 来源: haoxiai.net
相关推荐

2009-09-09 18:20:29

C# XML编程

2009-08-20 11:01:51

C#操作内存

2009-09-01 11:25:08

C#读取Word文件

2009-08-31 18:38:59

C#写文件

2009-08-18 16:49:05

C# 操作XML

2009-09-09 13:57:28

C# XML解析

2009-09-04 14:14:55

C#文档

2009-08-12 15:26:38

C#读取XML文档

2009-08-18 16:42:49

C# 操作XML

2009-08-26 11:53:56

C#打印文本文件

2009-09-09 14:40:15

C# XML解析

2024-04-03 00:10:24

C#System数据

2009-08-18 17:11:03

C#操作XML文件

2009-09-02 17:12:06

C#关机代码

2009-08-18 10:14:19

C#插件构架

2009-09-11 12:31:52

C#实例详解TypeConvert

2009-08-18 16:30:41

C# 操作XML

2009-09-09 15:54:48

C# XML序列化

2009-08-18 13:49:21

C# 操作Excel

2009-08-19 16:09:15

C#操作Access
点赞
收藏

51CTO技术栈公众号