欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net(C#) Xml操作(增删改查)练习

程序员文章站 2022-05-09 08:10:29
web.config配置: 复制代码 代码如下: web.config配置:
复制代码 代码如下:

<appsettings>
<add key="xmlfile" value="xml/class.xml"/>
</appsettings>
<appsettings>
<add key="xmlfile" value="xml/class.xml"/>
</appsettings>

前台:
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="test_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>c#操作xml(增删改查)练习</title>
</head>
<body>
<form id="form1" runat="server">
<div id="showxml" runat="server">
显示xml文档
</div>
<div style="background-color:green;color:yellow;" style="background-color:green;color:yellow;">为html控件绑定服务器控件的两个要点:<br />
1.onserverclick="servermethod"这里只写方法名.<br />
2.后台代码,必须是<br />
protected void xmladd(object sender, eventargs e){}<br />
注意两个参数及保护级.
</div>
<input id="btnadd" type="button" value="add" runat="server" onserverclick="xmladd" />
<input id="btndelete" type="button" value="delete" runat="server" onserverclick="xmldelete" />
<input id="btnupdate" type="button" value="update" runat="server" onserverclick="xmlupdate" />
<input id="btnquery" type="button" value="query" runat="server" onserverclick="xmlquery" />
</form>
</body>
</html>
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="test_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>c#操作xml(增删改查)练习</title>
</head>
<body>
<form id="form1" runat="server">
<div id="showxml" runat="server">
显示xml文档
</div>
<div style="background-color:green;color:yellow;" style="background-color:green;color:yellow;">为html控件绑定服务器控件的两个要点:<br />
1.onserverclick="servermethod"这里只写方法名.<br />
2.后台代码,必须是<br />
protected void xmladd(object sender, eventargs e){}<br />
注意两个参数及保护级.
</div>
<input id="btnadd" type="button" value="add" runat="server" onserverclick="xmladd" />
<input id="btndelete" type="button" value="delete" runat="server" onserverclick="xmldelete" />
<input id="btnupdate" type="button" value="update" runat="server" onserverclick="xmlupdate" />
<input id="btnquery" type="button" value="query" runat="server" onserverclick="xmlquery" />
</form>
</body>
</html>

后台:
复制代码 代码如下:

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

xml文件
复制代码 代码如下:

<?xml version="1.0" encoding="gb2312"?>
<root>
<student admin="no">
<name>rock</name>
<nickname>rock1</nickname>
<pwd>123</pwd>
<sex>男生</sex>
<birthday>1986-1-1</birthday>
<email>xymac@163.com</email>
<qq>123374355</qq>
<msn>loveplc@live.cn</msn>
<tel>13005129336</tel>
<homepage>//www.jb51.net</homepage>
<address>广州</address>
<work>asp.net菜鸟</work>
<photo>images/rock.gif</photo>
<time>2008-3-18 10:15:29</time>
</student>
<student admin="yes">
<name>tree</name>
<nickname>宿舍老大</nickname>
<pwd>51aspx</pwd>
<sex>男生</sex>
<birthday>
</birthday>
<email>support@tree.com</email>
<qq>
</qq>
<msn>
</msn>
<tel>
</tel>
<homepage>
</homepage>
<address>
</address>
<work>
</work>
<photo>
</photo>
<time>2008-3-26 11:39:57</time>
</student>
<student>
<name>tree2</name>
</student>
<student id="001">
<name>tree1</name>
</student>
</root>