建立空网页
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="eee.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">
请输入节点的内容<br>
学号:<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/>
地址:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br/>
城市:<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br/>
<asp:Button ID="Button1" runat="server" Text="创建新节点" οnclick="Button1_Click"/>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
后台代码;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace eee{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile1.xml"));
string str = "<ID>" + TextBox1.Text + "</ID>";
string str1 = "<NAME>" + TextBox2.Text + "</NAME>";
string str2 = "<Phone>" + TextBox3.Text + "</Phone>";
string str3 = "<Address>" + TextBox4.Text + "</Address>";
string str5 = "<City>" + TextBox5.Text + "</City>";
string str4 = "<DocumentElement>" + "<Students>" + str + str1 + str2 + str3 + str5+"</Students>" + "</DocumentElement>";
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(str4);
XmlNode node = doc.ImportNode(doc1.DocumentElement.LastChild, true);
doc.DocumentElement.AppendChild(node);
doc.Save(Server.MapPath("XMLFile1.xml"));
Label1.Text = "创建新节点成功!";
}
}
}
XMLFile1.xml文件的内容会有所增加:
<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
<Students>
<ID>3456</ID>
<NAME>xiaoli</NAME>
<Phone>139****</Phone>
<Address>纽约18大道</Address>
<City>纽约</City>
</Students>
<Students>
<ID>1234</ID>
<NAME>xiaoli</NAME>
<Phone>139****</Phone>
<Address>纽约18大道</Address>
<City>纽约</City>
</Students>
<Students>
<ID>55</ID>
<NAME>liheao</NAME>
<Phone>139</Phone>
<Address>dahua</Address>
<City>shenyang</City>
</Students>
<Students>
<ID>333</ID>
<NAME>lihexiang</NAME>
<Phone>123</Phone>
<Address>heping</Address>
<City>shenyabg</City>
</Students>
<Students>
<ID>1</ID>
<NAME>li</NAME>
<Phone>139</Phone>
<Address>东中街</Address>
<City>沈阳</City>
</Students>
</DocumentElement>