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(); } } |
时间: 2024-10-10 15:41:59