时间:2016-02-15 22:07 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net(C#) Xml操作(增删改查)练习】,下面是详细的讲解!
asp.net(C#) Xml操作(增删改查)练习
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class test_Default : System.Web.UI.Page
{
string xmlFile=System.Configuration.ConfigurationManager.AppSettings["xmlFile"];
XmlDocument XmlDoc=new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
XmlDoc.Load(Server.MapPath("../" + xmlFile));//向上一级
this.showXml.InnerHtml=System.Web.HttpUtility.HtmlEncode(XmlDoc.InnerXml);
}
protected void XmlAdd(object sender, EventArgs e)
{
XmlNode objRootNode=XmlDoc.SelectSingleNode("//Root"); //声明XmlNode对象
XmlElement objChildNode=XmlDoc.CreateElement("Student"); //创建XmlElement对象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement=XmlDoc.CreateElement("Name");//?结点和元素的区别?方法都一样.
objElement.InnerText="tree1";
objChildNode.AppendChild(objElement);
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlDelete(object sender, EventArgs e)
{
string Node="//Root/Student[Name='tree1']";//Xml是严格区分大小写的.
XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlUpdate(object sender, EventArgs e)
{
//XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']/Name").InnerText="tree2";
XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']").Attributes["id"].Value="001";
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlQuery(object sender, EventArgs e)
{
XmlNodeList NodeList=XmlDoc.SelectNodes("//Root/Student");//查询全部的student节点
//循环遍历节点,查询是否存在该节点
for (int i=0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查询单个节点,//表示全部匹配的元素./表示以此为根的子元素.javascript下的查询也是一样.
string XmlPathNode="//Root/Student[Name='rock']/Photo";
Response.Write(XmlDoc.SelectSingleNode(XmlPathNode).InnerText);
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class test_Default : System.Web.UI.Page
{
string xmlFile=System.Configuration.ConfigurationManager.AppSettings["xmlFile"];
XmlDocument XmlDoc=new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
private void Bind()
{
XmlDoc.Load(Server.MapPath("../" + xmlFile));//向上一级
this.showXml.InnerHtml=System.Web.HttpUtility.HtmlEncode(XmlDoc.InnerXml);
}
protected void XmlAdd(object sender, EventArgs e)
{
XmlNode objRootNode=XmlDoc.SelectSingleNode("//Root"); //声明XmlNode对象
XmlElement objChildNode=XmlDoc.CreateElement("Student"); //创建XmlElement对象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement=XmlDoc.CreateElement("Name");//?结点和元素的区别?方法都一样.
objElement.InnerText="tree1";
objChildNode.AppendChild(objElement);
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlDelete(object sender, EventArgs e)
{
string Node="//Root/Student[Name='tree1']";//Xml是严格区分大小写的.
XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlUpdate(object sender, EventArgs e)
{
//XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']/Name").InnerText="tree2";
XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']").Attributes["id"].Value="001";
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlQuery(object sender, EventArgs e)
{
XmlNodeList NodeList=XmlDoc.SelectNodes("//Root/Student");//查询全部的student节点
//循环遍历节点,查询是否存在该节点
for (int i=0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查询单个节点,//表示全部匹配的元素./表示以此为根的子元素.javascript下的查询也是一样.
string XmlPathNode="//Root/Student[Name='rock']/Photo";
Response.Write(XmlDoc.SelectSingleNode(XmlPathNode).InnerText);
}
}
关于asp.net(C#) Xml操作(增删改查)练习的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【asp】asp.net ubb使用代码-net-ubb使用
- 【默认图片】图片不存在使用默认图片代替的实例
- 【页面打印】关于ASP.NET页面打印技术的常用方法
- 【MVC5】MVC 5 第一章 创建MVC 5 web应用程序-net
- 【MVC】一个简单MVC5 + EF6示例分享-EF6实例-MVC5
- 【服务器】asp.net页面状态管理cookie和服务器状
- 【asp】asp.net更新指定记录的方法-net--更新-指
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
