All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
DataGrid单击一行服务器事件_[Asp.Net教程]
使用,C#,编程对,RTF,文档进行操作_[Asp.Net教程]
PHP取得当前插入数据库中记录的id值_php资料_编程技术
SQL注入漏洞全接触(进阶篇)_[SQL,Server教程]
.Net中FckEditor的配置和使用方法(含示例源码)_[Asp.Net教程]
GridView显示服务器图片(保存为图片路径)_[Asp.Net教程]
黑客教程系列之微型PHP木马的探讨_php资料_编程技术
一些Select检索高级用法(适用于mssql)_[SQL,Server教程]
SQL,SERVER,DBCC命令解释_mssql学习_编程技术
网页技巧:如何在网页上实现音乐播放?_JavaScript技术_编程技术
Reading number is top 10 pictures
The world's top ten most beautiful railway station2
China telecom 114 spokesman MeiYanXu2
The money of more than 100 countries and regions20
2012 national geographic daily picture7
Beauty ZhiHuiLin1
Fan bingbing black wings for platform and DanLuoWang believes beauty2
这才是真正的人体艺术5
可爱的狗熊们
原子弹轰炸长崎的珍贵照片
A man's favorite things2
Download software ranking
C++教程第四版
Visual C++界面编程技术
Red cliff
Unix video tutorial12
都市狐狸姑娘传
dreamweaver8中文版
Sora aoi‘s film--Lust fan wall
Unix video tutorial17
Boxer vs Yellow4
仙剑奇侠传98硬盘WINXP版
aaa published in(发表于) 2013/12/8 7:51:11 Edit(编辑)
C#操作xml文件入门_.net资料_编程技术

C#操作xml文件入门_.net资料_编程技术

C#操作xml文件入门_.net资料_编程技术-你的首页-uuhomepage.com

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





Oberon's Legacy
Corets, Eva
5.95


1、往节点中插入一个节点:


XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("bookstore.xml");
XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找
XmlElement xe1=xmlDoc.CreateElement("book");//创建一个节点
xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性


XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS从入门到精通";//设置文本节点
xe1.AppendChild(xesub1);//添加到节点中
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);//添加到节点中
xmlDoc.Save("bookstore.xml");


//================
结果为:





Oberon's Legacy
Corets, Eva
5.95


CS从入门到精通
候捷
58.3


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


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");//保存。


//=================


最后结果为:





Oberon's Legacy
Corets, Eva
5.95


CS从入门到精通
亚胜
58.3


3、删除 节点的genre属性,删除 节点。


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");


//====================


最后结果为:





Oberon's Legacy
Corets, Eva
5.95




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);//显示子节点点文本
}
}






添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.