首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视 ┆淘宝手机在线充值 ┆淘宝游戏点卡充值 
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>NET专区>ASP.NET>asp.net操作xml文件的代码实例
asp.net操作xml文件的代码实例
来源: 发布时间:2007-12-25 发布人: 浏览: 人次   字体: [ ]  
在开始之前,先建立一个smallfools.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<smallfoolsRoot>
   <poems>
     <author>王维</author>
     <title>竹里馆</title>
     <content>独坐幽篁里,弹琴复长啸。深林人不知,明月来相照。</content>
   </poems>
   <poems>
     <author>孟浩然</author>
     <title>宿建德江</title>
     <content>移舟泊烟渚,日暮客愁新。野旷天低树,江清月近人</content>
   </poems>
   <poems>
     <author>李白</author>
     <title>杜陵绝句</title>
     <content>南登杜陵上,北望五陵间。秋水明落日,流光灭远山</content>
   </poems>
   <poems>
     <author>李白</author>
     <title>望庐山瀑布</title>
     <content>日照香炉生紫烟,遥看瀑布挂前川。飞流直下三千尺,疑是银河落九天。</content>
   </poems>
   <poems>
     <author>李商隐</author>
     <title>锦瑟</title>
     <content>锦瑟无端五十弦,一弦一柱思华年。庄生晓梦迷蝴蝶,望帝春心托杜鹃。沧海月明珠有泪,蓝田日暖玉生烟。此情可待成追忆,只是当时已惘然。</content>
   </poems>
</smallfoolsRoot>

     下面的操作都在这个xml文件里进行。

操作一:读取整个XML文件,并在DataGrid里显示出来:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("smallfools.xml"));
if (ds.Tables.Count>0)
{
this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
}

操作二:获得第一个节点的值

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.FirstChild;
if (xmlNode!=null)
{
this.tbauthor.Text = xmlNode["author"].InnerText;
this.tbtitle.Text = xmlNode["title"].InnerText;
this.tbcontent.Text = xmlNode["content"].InnerText;
ViewState["Count"] = 0;
}

操作三:查看某一个节点的内容
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNodeList xmlNodeList = xmlDoc.DocumentElement.ChildNodes;
XmlNode xmlNode = xmlNodeList.Item(0);
this.tbauthor.Text = xmlNode["author"].InnerText;
this.tbtitle.Text = xmlNode["title"].InnerText;
this.tbcontent.Text = xmlNode["content"].InnerText;

操作四:添加一个节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
//创建一个新节点
XmlElement newElement = xmlDoc.CreateElement("poems");
//创建newElement下的节点
XmlElement elauthor = xmlDoc.CreateElement("author");
XmlElement eltitle = xmlDoc.CreateElement("title");
XmlElement elcontent = xmlDoc.CreateElement("content");
elauthor.InnerText = this.tbaddauthor.Text.Trim();
eltitle.InnerText = this.tbaddtitle.Text.Trim();
elcontent.InnerText = this.tbaddcontent.Text.Trim();
//将newElement下的节点加到newElement上
newElement.AppendChild(elauthor);
newElement.AppendChild(eltitle);
newElement.AppendChild(elcontent);
//将newElement加入到xml文件中(加在最后一条记录上)
xmlDoc.DocumentElement.AppendChild(newElement);
//如果要插到某条记录之后也可以用(加在第一条记录之后)
//xmlDoc.DocumentElement.InsertAfter(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));
//如果要插到某条记录之前也可以用(加在第一条记录之前)
//xmlDoc.DocumentElement.InsertBefore(newElement,xmlDoc.DocumentElement.ChildNodes.Item(0));
//存盘
xmlDoc.Save(Server.MapPath("smallfools.xml"));

操作五:删除某个节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(0);
xmlNode.ParentNode.RemoveChild(xmlNode);
xmlDoc.Save(Server.MapPath("smallfools.xml"));

操作六:编辑某个节点
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
//获得节点列表
XmlNode xmlNode = xmlDoc.DocumentElement.ChildNodes.Item(1);
xmlNode["author"].InnerText = this.tbauthor.Text;
xmlNode["title"].InnerText = this.tbtitle.Text;
xmlNode["content"].InnerText = this.tbcontent.Text;
xmlDoc.Save(Server.MapPath("smallfools.xml"));

操作七:查找记录
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("smallfoolsRoot/poems[author='"+this.tbsearch.Text.Trim()+"']");

操作八:糊模查找记录
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("smallfools.xml"));
XmlNodeList nodelist = xmlDoc.SelectNodes("smallfoolsRoot/poems[contains(author,'"+this.tbsearch.Text.Trim()+"')]");


相 关 文 章   发布商链接
·ASP.NET程序开发中经典常用的三十三...
·asp.net生成缩略图代码实例(可按百...
·在ASP.NET中实现跨页面多选的代码
·asp.net实现url传递中文的解决方案
·asp.net使用checkbox控制datagrid行...
·asp.net操作Excel,读取、导出,操作...
·asp.net页面回传与js调用服务端事件...
·asp.net中"已有打开的与此命令相关联...
·如何在asp.net中编写 Windows 服务程...
·asp.net实现动态添加控件并添加事件...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·实例讲解ASP.NET实现加密Cook...
·如何实现在DataList控件中的D...
·实例讲解asp.net生成高质量缩
·asp.net适用于IE或FireFox的...
·举例详解C#代码与javaScript...
·asp.net无刷新上传文件
·在Ie中保存图片时出现"800700...
·asp.net文件上传大小限制的控
·asp.net动态生成txt文本文件...
·asp.net实现将Excel文件导入...
·实现多列,带图片的DropDownLi...
·javascript实现TreeView 控件
·ASP.NET配置Word的操作权限
·让FileUpload控件在IE和FireF...
·ASP.NET实现Office文档的分类
热 门 文 章
·asp.net(C#)上传下载及文件管...
·图解asp.net如何用excel做报...
·asp.net无刷新上传文件
·Asp.Net防止刷新重复提交数据...
·asp.net实现将Excel文件导入...
·ASP.NET取得物理路径和虚拟路...
·asp.net中Web.Config配置文件...
·asp.net(c#)生成验证码代码,...
·asp.net页面回传与js调用服务...
·asp.net中DataBinder.Eval的...
·asp.net(C#版)实现登录验证码...
·DataGrid中DropDownList触发S...
·asp.net可输入的下拉框复合控...
·ASP.net 实现批量数据更新或...
·asp.net中常见的几种日历控件...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .