xml-5处理

建立空网页

<%@ 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"/>
    &nbsp;&nbsp; <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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值