asp.net对XML文件的一些简单操作
2007-11-26 19:28
429 查看
在开始之前,先建立一个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()+"')]");
<?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对XML文件的一些简单操作
- asp.net对XML文件的一些简单操作(转)
- asp.net中对XML文件的简单操作
- ASP.NET中的一些字符串操作
- 在asp.net中操作sql-server数据库的一些小技巧
- asp.net 对xml文件的读写,添加,修改,删除操作
- [ASP.net]Context的一些简单运用
- C#,Dictionary,asp.net 字典 用法及简单操作
- asp.net操作xml文件的代码实例
- asp.net 对xml文件的读写,添加,修改,删除操作
- ASP.Net 更新储存在xml文件中的数据(update操作)
- asp.net TreeView 一些操作
- asp.net简单生成XML文件的方法
- Asp.Net小项目的小项目-实现图书管理系统中对读者的一些操作
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- 在asp.net中操作sql server数据库的一些小技巧
- asp.net 对xml文件的读写,添加,修改,删除操作
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- asp.net TreeView 一些操作
- 编写数据库操作类,使ASP.NET中的数据库操作变得简单