asp.net(C#) Xml操作(增刪改查)練習(xí)
<appSettings>
<add key="xmlFile" value="xml/class.xml"/>
</appSettings>
<appSettings>
<add key="xmlFile" value="xml/class.xml"/>
</appSettings>
前臺(tái):
<%@ 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(增刪改查)練習(xí)</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控件綁定服務(wù)器控件的兩個(gè)要點(diǎn):<br />
1.onserverclick="serverMethod"這里只寫方法名.<br />
2.后臺(tái)代碼,必須是<br />
protected void XmlAdd(object sender, EventArgs e){}<br />
注意兩個(gè)參數(shù)及保護(hù)級(jí).
</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(增刪改查)練習(xí)</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控件綁定服務(wù)器控件的兩個(gè)要點(diǎn):<br />
1.onserverclick="serverMethod"這里只寫方法名.<br />
2.后臺(tái)代碼,必須是<br />
protected void XmlAdd(object sender, EventArgs e){}<br />
注意兩個(gè)參數(shù)及保護(hù)級(jí).
</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>
后臺(tái):
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));//向上一級(jí)
this.showXml.InnerHtml = System.Web.HttpUtility.HtmlEncode(XmlDoc.InnerXml);
}
protected void XmlAdd(object sender, EventArgs e)
{
XmlNode objRootNode = XmlDoc.SelectSingleNode("http://Root"); //聲明XmlNode對(duì)象
XmlElement objChildNode = XmlDoc.CreateElement("Student"); //創(chuàng)建XmlElement對(duì)象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement = XmlDoc.CreateElement("Name");//???結(jié)點(diǎn)和元素的區(qū)別?方法都一樣.
objElement.InnerText = "tree1";
objChildNode.AppendChild(objElement);
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlDelete(object sender, EventArgs e)
{
string Node = "http://Root/Student[Name='tree1']";//Xml是嚴(yán)格區(qū)分大小寫的.
XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlUpdate(object sender, EventArgs e)
{
//XmlDoc.SelectSingleNode("http://Root/Student[Name='tree1']/Name").InnerText = "tree2";
XmlDoc.SelectSingleNode("http://Root/Student[Name='tree1']").Attributes["id"].Value = "001";
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlQuery(object sender, EventArgs e)
{
XmlNodeList NodeList = XmlDoc.SelectNodes("http://Root/Student");//查詢?nèi)康膕tudent節(jié)點(diǎn)
//循環(huán)遍歷節(jié)點(diǎn),查詢是否存在該節(jié)點(diǎn)
for (int i = 0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查詢單個(gè)節(jié)點(diǎn),//表示全部匹配的元素./表示以此為根的子元素.javascript下的查詢也是一樣.
string XmlPathNode = "http://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));//向上一級(jí)
this.showXml.InnerHtml = System.Web.HttpUtility.HtmlEncode(XmlDoc.InnerXml);
}
protected void XmlAdd(object sender, EventArgs e)
{
XmlNode objRootNode = XmlDoc.SelectSingleNode("http://Root"); //聲明XmlNode對(duì)象
XmlElement objChildNode = XmlDoc.CreateElement("Student"); //創(chuàng)建XmlElement對(duì)象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement = XmlDoc.CreateElement("Name");//???結(jié)點(diǎn)和元素的區(qū)別?方法都一樣.
objElement.InnerText = "tree1";
objChildNode.AppendChild(objElement);
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlDelete(object sender, EventArgs e)
{
string Node = "http://Root/Student[Name='tree1']";//Xml是嚴(yán)格區(qū)分大小寫的.
XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlUpdate(object sender, EventArgs e)
{
//XmlDoc.SelectSingleNode("http://Root/Student[Name='tree1']/Name").InnerText = "tree2";
XmlDoc.SelectSingleNode("http://Root/Student[Name='tree1']").Attributes["id"].Value = "001";
//保存
XmlDoc.Save(Server.MapPath("../" + xmlFile));
}
protected void XmlQuery(object sender, EventArgs e)
{
XmlNodeList NodeList = XmlDoc.SelectNodes("http://Root/Student");//查詢?nèi)康膕tudent節(jié)點(diǎn)
//循環(huán)遍歷節(jié)點(diǎn),查詢是否存在該節(jié)點(diǎn)
for (int i = 0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查詢單個(gè)節(jié)點(diǎn),//表示全部匹配的元素./表示以此為根的子元素.javascript下的查詢也是一樣.
string XmlPathNode = "http://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>http://chabaoo.cn</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>
相關(guān)文章
.net MVC 連接數(shù)據(jù)本地?cái)?shù)據(jù)庫(kù)三種方法總結(jié)
這篇文章主要介紹了.net MVC 連接數(shù)據(jù)本地?cái)?shù)據(jù)庫(kù)三種方法總結(jié)的相關(guān)資料,這里附有代碼實(shí)例,需要的朋友可以參考下2016-12-12ASP.NET Core利用Jaeger實(shí)現(xiàn)分布式追蹤詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core利用Jaeger實(shí)現(xiàn)分布式追蹤的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04.NET 6開發(fā)TodoList應(yīng)用之實(shí)現(xiàn)PUT請(qǐng)求
PUT請(qǐng)求本身其實(shí)可說(shuō)的并不多,過(guò)程也和創(chuàng)建基本類似。這篇文章主要為大家介紹了.NET6實(shí)現(xiàn)PUT請(qǐng)求的示例詳解,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12微信公眾平臺(tái)開發(fā)之語(yǔ)音識(shí)別.Net代碼解析
這篇文章主要為大家詳細(xì)解析了微信公眾平臺(tái)開發(fā)之語(yǔ)音識(shí)別.Net代碼,感興趣的小伙伴們可以參考一下2016-06-06the sourcesafe database has been locked by the administrator
今天早上打開soucesafe的時(shí)候出現(xiàn)提示:“the sourcesafe database has been locked by the administrator"。仔細(xì)想想, 可能是前天晚上用"f:\analyze.exe" -I- -DB -F -V3 -D "f:\vssData\data" 命今分析的時(shí)候鎖定了database2009-04-04.NET?SkiaSharp?生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)
這篇文章主要為大家介紹了.NET?SkiaSharp?生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10ASP.NET MVC4中使用Html.DropDownListFor的方法示例
這篇文章主要介紹了ASP.NET MVC4中使用Html.DropDownListFor的方法,結(jié)合實(shí)例形式分析了控制器數(shù)據(jù)源及Html.DropDownListFor顯示操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08