asp.net(C#) Xml操作(增刪改查)練習
更新時間:2009年01月30日 18:34:06 作者:
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 />
注意兩個參數(shù)及保護級.
</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 />
注意兩個參數(shù)及保護級.
</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("http://Root"); //聲明XmlNode對象
XmlElement objChildNode = XmlDoc.CreateElement("Student"); //創(chuàng)建XmlElement對象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement = XmlDoc.CreateElement("Name");//???結(jié)點和元素的區(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是嚴格區(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é)點
//循環(huán)遍歷節(jié)點,查詢是否存在該節(jié)點
for (int i = 0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查詢單個節(jié)點,//表示全部匹配的元素./表示以此為根的子元素.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));//向上一級
this.showXml.InnerHtml = System.Web.HttpUtility.HtmlEncode(XmlDoc.InnerXml);
}
protected void XmlAdd(object sender, EventArgs e)
{
XmlNode objRootNode = XmlDoc.SelectSingleNode("http://Root"); //聲明XmlNode對象
XmlElement objChildNode = XmlDoc.CreateElement("Student"); //創(chuàng)建XmlElement對象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement = XmlDoc.CreateElement("Name");//???結(jié)點和元素的區(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是嚴格區(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é)點
//循環(huán)遍歷節(jié)點,查詢是否存在該節(jié)點
for (int i = 0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查詢單個節(jié)點,//表示全部匹配的元素./表示以此為根的子元素.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://www.dbjr.com.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>
復制代碼 代碼如下:
<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 />
注意兩個參數(shù)及保護級.
</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 />
注意兩個參數(shù)及保護級.
</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("http://Root"); //聲明XmlNode對象
XmlElement objChildNode = XmlDoc.CreateElement("Student"); //創(chuàng)建XmlElement對象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement = XmlDoc.CreateElement("Name");//???結(jié)點和元素的區(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是嚴格區(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é)點
//循環(huán)遍歷節(jié)點,查詢是否存在該節(jié)點
for (int i = 0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查詢單個節(jié)點,//表示全部匹配的元素./表示以此為根的子元素.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));//向上一級
this.showXml.InnerHtml = System.Web.HttpUtility.HtmlEncode(XmlDoc.InnerXml);
}
protected void XmlAdd(object sender, EventArgs e)
{
XmlNode objRootNode = XmlDoc.SelectSingleNode("http://Root"); //聲明XmlNode對象
XmlElement objChildNode = XmlDoc.CreateElement("Student"); //創(chuàng)建XmlElement對象
objChildNode.SetAttribute("id", "1");
objRootNode.AppendChild(objChildNode);
//
XmlElement objElement = XmlDoc.CreateElement("Name");//???結(jié)點和元素的區(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是嚴格區(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é)點
//循環(huán)遍歷節(jié)點,查詢是否存在該節(jié)點
for (int i = 0; i < NodeList.Count; i++)
{
Response.Write(NodeList[i].ChildNodes[0].InnerText);
}
//查詢單個節(jié)點,//表示全部匹配的元素./表示以此為根的子元素.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://www.dbjr.com.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ù)本地數(shù)據(jù)庫三種方法總結(jié)
這篇文章主要介紹了.net MVC 連接數(shù)據(jù)本地數(shù)據(jù)庫三種方法總結(jié)的相關(guān)資料,這里附有代碼實例,需要的朋友可以參考下2016-12-12ASP.NET Core利用Jaeger實現(xiàn)分布式追蹤詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core利用Jaeger實現(xiàn)分布式追蹤的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04.NET 6開發(fā)TodoList應用之實現(xiàn)PUT請求
PUT請求本身其實可說的并不多,過程也和創(chuàng)建基本類似。這篇文章主要為大家介紹了.NET6實現(xiàn)PUT請求的示例詳解,感興趣的小伙伴可以跟隨小編一起學習一下2021-12-12the sourcesafe database has been locked by the administrator
今天早上打開soucesafe的時候出現(xiàn)提示:“the sourcesafe database has been locked by the administrator"。仔細想想, 可能是前天晚上用"f:\analyze.exe" -I- -DB -F -V3 -D "f:\vssData\data" 命今分析的時候鎖定了database2009-04-04.NET?SkiaSharp?生成二維碼驗證碼及指定區(qū)域截取方法實現(xiàn)
這篇文章主要為大家介紹了.NET?SkiaSharp?生成二維碼驗證碼及指定區(qū)域截取方法實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10ASP.NET MVC4中使用Html.DropDownListFor的方法示例
這篇文章主要介紹了ASP.NET MVC4中使用Html.DropDownListFor的方法,結(jié)合實例形式分析了控制器數(shù)據(jù)源及Html.DropDownListFor顯示操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08