• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
无题
博客园    首页    新随笔    联系   管理    订阅  订阅

C#操作xml文件入门

  1Posted on 2006-10-10 17:10 小隐任行 阅读(305) 评论(0)  编辑  收藏 所属分类: 编程资料  
  2已知有一个XML文件(bookstore.xml)如下:
  3
  4
  5<?xml version="1.0" encoding="gb2312"?>
  6<bookstore>
  7<book genre="fantasy" ISBN="2-3631-4">
  8<title>Oberon's Legacy</title>
  9<author>Corets, Eva</author>
 10<price>5.95</price>
 11</book>
 12</bookstore>
 13
 14
 15
 16
 17
 181、往<bookstore>节点中插入一个<book>节点:
 19
 20
 21
 22
 23XmlDocument xmlDoc=new XmlDocument();
 24xmlDoc.Load("bookstore.xml");
 25XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
 26XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
 27xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
 28xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性
 29
 30XmlElement xesub1=xmlDoc.CreateElement("title");
 31xesub1.InnerText="CS从入门到精通";//设置文本节点
 32xe1.AppendChild(xesub1);//添加到<book>节点中
 33XmlElement xesub2=xmlDoc.CreateElement("author");
 34xesub2.InnerText="候捷";
 35xe1.AppendChild(xesub2);
 36XmlElement xesub3=xmlDoc.CreateElement("price");
 37xesub3.InnerText="58.3";
 38xe1.AppendChild(xesub3);
 39
 40root.AppendChild(xe1);//添加到<bookstore>节点中
 41xmlDoc.Save("bookstore.xml");
 42
 43
 44
 45
 46
 47//================
 48结果为:
 49
 50
 51
 52
 53<?xml version="1.0" encoding="gb2312"?>
 54<bookstore>
 55<book genre="fantasy" ISBN="2-3631-4">
 56<title>Oberon's Legacy</title>
 57<author>Corets, Eva</author>
 58<price>5.95</price>
 59</book>
 60<book genre="李赞红" ISBN="2-3631-4">
 61<title>CS从入门到精通</title>
 62<author>候捷</author>
 63<price>58.3</price>
 64</book>
 65</bookstore>
 66
 67
 68
 69
 70
 712、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
 72
 73
 74XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
 75foreach(XmlNode xn in nodeList)//遍历所有子节点
 76{
 77XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
 78if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
 79{
 80xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”
 81
 82XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
 83foreach(XmlNode xn1 in nls)//遍历
 84{
 85XmlElement xe2=(XmlElement)xn1;//转换类型
 86if(xe2.Name=="author")//如果找到
 87{
 88xe2.InnerText="亚胜";//则修改
 89break;//找到退出来就可以了
 90}
 91}
 92break;
 93}
 94}
 95
 96xmlDoc.Save("bookstore.xml");//保存。
 97
 98
 99
100
101//=================
102
103最后结果为:
104
105
106<?xml version="1.0" encoding="gb2312"?>
107<bookstore>
108<book genre="fantasy" ISBN="2-3631-4">
109<title>Oberon's Legacy</title>
110<author>Corets, Eva</author>
111<price>5.95</price>
112</book>
113<book genre="update李赞红" ISBN="2-3631-4">
114<title>CS从入门到精通</title>
115<author>亚胜</author>
116<price>58.3</price>
117</book>
118</bookstore>
119
120
121
122
1233、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
124
125
126XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;
127
128foreach(XmlNode xn in xnl)
129{
130XmlElement xe=(XmlElement)xn;
131
132
133if(xe.GetAttribute("genre")=="fantasy")
134{
135xe.RemoveAttribute("genre");//删除genre属性
136}
137else if(xe.GetAttribute("genre")=="update李赞红")
138{
139xe.RemoveAll();//删除该节点的全部内容
140}
141}
142xmlDoc.Save("bookstore.xml");
143
144//====================
145
146最后结果为:
147
148
149<?xml version="1.0" encoding="gb2312"?>
150<bookstore>
151<book ISBN="2-3631-4">
152<title>Oberon's Legacy</title>
153<author>Corets, Eva</author>
154<price>5.95</price>
155</book>
156<book>
157</book>
158</bookstore> 
159
160
1614、显示所有数据。
162
163
164XmlNode xn=xmlDoc.SelectSingleNode("bookstore");
165
166XmlNodeList xnl=xn.ChildNodes;
167
168foreach(XmlNode xnf in xnl)
169{
170XmlElement xe=(XmlElement)xnf;
171Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
172Console.WriteLine(xe.GetAttribute("ISBN"));
173
174XmlNodeList xnf1=xe.ChildNodes;
175foreach(XmlNode xn2 in xnf1)
176{
177Console.WriteLine(xn2.InnerText);//显示子节点点文本
178}
179} 
180
posted @ 2007-11-14 23:05  Borcala  阅读(484)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3