电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程
XML的简单读取与写入-XML教程,XML基础
作者:网友供稿 点击:48
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[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> 
</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.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"); 
//=============================================== 
结果为: 
<?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");//保存。 
//================================================== 
最后结果为: 
<?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">节点。 
xmlnodelist 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"); 
//=========================================== 
最后结果为: 
<?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);//显示子节点点文本 


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·跟我学xml和XSL-XML教程,XML工具及环境
·精彩:用Asp实现QQ在线查询
·XMLDOM对象方法:对象事件-XML教程,XML基础
·使用XML DOM生成XML(3)-XML教程,DOM/SAX
·跟我学XSL(一)-XML教程,样式表技术
·使用XML DOM生成XML(4)-XML教程,DOM/SAX
·XML的简单读取与写入-XML教程,XML基础
·跟我学XSL(三)-XML教程,样式表技术
·了解WEB页面工具语言XML(二)定义-XML教程,XML工具及环境
·了解WEB页面工具语言XML(五)好处-XML教程,XML工具及环境

最新文章
·ChinaCache CDN门户类网站加速方案
·UniCache 行业垂直门户网站加速方案
·UniCache 博客 论坛 WEB2.0加速方案
·UniCache 播客网站CDN加速解决方案
·ChinaCache CDN中型企业类网站方案
·ChinaCache CDN大型企业类网站方案
·ChinaCache CDN电子商务类网站方案
·ChinaCache CDN Web2.0网站加速方案
·ChinaCache CDN软件类网站加速方案
·ChinaCache CDN游戏类网站加速方案


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号