ASP.NET學習筆記之XmlDocument操作XML

來源:互聯網
上載者:User

1:實驗目的,實驗要求(自己寫)
2:實驗步驟:

一:Xml是實現病曆共用的另外一種較好的方式。C#通過System.Xml空間下的一系列類來處理XML文檔,如何使用這些類呢?
在應用程式的頭部添加:
using System.Xml
二:建立patient.xml文檔

 代碼如下 複製代碼

<?xml version="1.0" encoding="gb2312"?>
<patientsmaterial>
</patientsmaterial>

1:實驗目的,實驗要求(自己寫)
2:實驗步驟:
一:Xml是實現病曆共用的另外一種較好的方式。C#通過System.Xml空間下的一系列類來處理XML文檔,如何使用這些類呢?
在應用程式的頭部添加:
using System.Xml
二:建立patient.xml文檔
<?xml version="1.0" encoding="gb2312"?>
<patientsmaterial>
</patientsmaterial>

三:建立ASP.NET應用程式,並在表單上添加兩個BUTTON按鈕,5個TextBox,如圖:

給出代碼:
Default.aspx

 代碼如下 複製代碼
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
ID:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
姓名:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
性別:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
E-mail:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
地址:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
<br />
 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="輸出所有節點(Button1)" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="添加一個節點(Button2)" /></div>
</form>
</body>
</html>

Default.aspx.cs

 代碼如下 複製代碼
using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
//注意修改為自己patient.xml檔案的路徑
string filePath = "C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite5\patient.xml";
protected void Page_Load(object sender, EventArgs e)
{
}
//輸出所有節點
public void getAllElements() {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNodeList nodeList = xmlDoc.SelectNodes("/patientsmaterial/patient");
Response.Write("所有節點:");
for (int i = 0; i < nodeList.Count; i++)
{
Response.Write("ID:"+nodeList.Item(i).Attributes["ID"].Value);//輸出ID
Response.Write("");
for (int j = 0; j < nodeList.Item(i).ChildNodes.Count; j++)
{
Response.Write(nodeList.Item(i).ChildNodes[j].Name + ":");
Response.Write(nodeList.Item(i).ChildNodes[j].InnerText);
Response.Write("");
}
Response.Write("");
}
}
protected void Button1_Click(object sender, EventArgs e)
{
getAllElements();
}
protected void Button2_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
//尋找
XmlNode root = xmlDoc.SelectSingleNode("patientsmaterial");
//建立patient節點
XmlElement xe1 = xmlDoc.CreateElement("patient");
//設定節點ID屬性
xe1.SetAttribute("ID", TextBox1.Text.Trim());
XmlElement xesub1 = xmlDoc.CreateElement("name");
//設定文本節點
xesub1.InnerText = TextBox2.Text.Trim();
//添加到patient節點去
xe1.AppendChild(xesub1);
XmlElement xesub2 = xmlDoc.CreateElement("sex");
xesub2.InnerText = TextBox3.Text.Trim();
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmlDoc.CreateElement("email");
xesub3.InnerText = TextBox4.Text.Trim();
xe1.AppendChild(xesub3);
XmlElement xesub4 = xmlDoc.CreateElement("address");
xesub4.InnerText = TextBox5.Text.Trim();
xe1.AppendChild(xesub4);
//添加到patientsmaterial節點
root.AppendChild(xe1);
xmlDoc.Save(filePath);
getAllElements();
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.