Asp. NET Learning notes XmlDocument manipulating XML

Source: Internet
Author: User
Tags trim visual studio

1: Experimental purposes, experimental requirements (own writing)
2: Experiment steps:

One: XML is another good way to realize the sharing of medical records. How do you use C # to work with XML documents by System.Xml a series of classes in a space?
Add to the head of the application:
Using System.Xml
Two: New Patient.xml document

The code is as follows Copy Code

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

1: Experimental purposes, experimental requirements (own writing)
2: Experiment steps:
One: XML is another good way to realize the sharing of medical records. How do you use C # to work with XML documents by System.Xml a series of classes in a space?
Add to the head of the application:
Using System.Xml
Two: New Patient.xml document
<?xml version= "1.0" encoding= "gb2312"?>
<patientsmaterial>
</patientsmaterial>

Three: Create the ASP.net application and add two button buttons to the form, 5 textbox, as shown in figure:

Give the code:
Default.aspx

The code is as follows Copy Code
<%@ 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 ">
<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
Id:<asp:textbox id= "TextBox1" runat= "Server" ></asp:textbox><br/>
Name: <asp:textbox id= "TextBox2" runat= "Server" ></asp:textbox><br/>
Sex: <asp:textbox id= "TextBox3" runat= "Server" ></asp:textbox><br/>
E-mail:<asp:textbox id= "TextBox4" runat= "Server" ></asp:textbox><br/>
Address: <asp:textbox id= "TEXTBOX5" runat= "Server" ></asp:textbox><br/>
<br/>
<asp:button id= "Button1" runat= "onclick=" button1_click "text=" Output all nodes (Button1) "/>"
<asp:button id= "Button2" runat= "onclick=" button2_click "text=" Add a Node (Button2) "/></div>
</form>
</body>

Default.aspx.cs

The code is as follows Copy Code
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
{
Note Modifying the path to your Patient.xml file
String FilePath = "C:\Documents and Settings\Administrator\My documents\visual Studio 2005\websites\website5\ Patient.xml ";
protected void Page_Load (object sender, EventArgs e)
{
}
Output all nodes
public void Getallelements () {
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (FilePath);
XmlNodeList nodelist = xmldoc.selectnodes ("/patientsmaterial/patient");
Response.Write ("All Nodes:");
for (int i = 0; i < Nodelist.count; i++)
{
Response.Write ("ID:" +nodelist.item (i). attributes["ID"]. Value);//Output 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);
Find
XmlNode root = Xmldoc.selectsinglenode ("patientsmaterial");
Creating Patient Nodes
XmlElement xe1 = xmldoc.createelement ("patient");
Set Node ID properties
Xe1. SetAttribute ("ID", TextBox1.Text.Trim ());
XmlElement xesub1 = xmldoc.createelement ("name");
Set text node
Xesub1. innertext = TextBox2.Text.Trim ();
Add to patient node to
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);
Add to Patientsmaterial node
Root. AppendChild (XE1);
Xmldoc.save (FilePath);
Getallelements ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.