asp.net 4.5 exercise ~ test15-5 xml create node

xmlfile1.xml

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<xinwen>
  <news>
    <news_id>1</news_id>
    <news_title>上海国际艺术节开幕</news_title>
    <news_author>王建宁</news_author>
    <news_ly>原创</news_ly>
    <news_content>第十三届中国上海国际艺术节将于11月18日举办。</news_content>
    <news_adddate>2013-10-18</news_adddate>
  </news>
  <news>
    <news_id>2</news_id>
    <news_title>上海首批公租房下月起招租</news_title>
    <news_author>王水宁</news_author>
    <news_ly>转载</news_ly>
    <news_content>两小区分别位于徐汇华庭和上体馆附近。</news_content>
    <news_adddate>2013-10-18</news_adddate>
  </news>
</xinwen>

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test15_5.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h3>请输入节点的内容</h3>
        编号:<asp:TextBox ID="txtId" runat="server"></asp:TextBox><br />
        标题:<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox><br />
        类别:<asp:TextBox ID="txtType" runat="server"></asp:TextBox><br />
        作者:<asp:TextBox ID="txtAuthor" runat="server"></asp:TextBox><br />
        内容:<asp:TextBox ID="txtContent" runat="server"></asp:TextBox><br />
        时间:<asp:TextBox ID="txtDate" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="创建" OnClick="Button1_Click" /><br />
        <asp:Label ID="lblTips" runat="server" Text=""></asp:Label><br />
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test15_5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string str1 = "";
            str1 += "<xinwen>";
            str1 += "<news>";
            str1 += "<news_id>" + txtId.Text.Trim() + "</news_id>";
            str1 += "<news_title>" + txtTitle.Text.Trim() + "</news_title>";
            str1 += "<news_author>" + txtAuthor.Text.Trim() + "</news_author>";
            str1 += "<news_ly>" + txtType.Text.Trim() + "</news_ly>";
            str1 += "<news_content>" + txtContent.Text.Trim() + "</news_content>";
            str1 += "<news_adddate>" + txtDate.Text.Trim() + "</news_adddate>";
            str1 += "</news>";
            str1 += "</xinwen>";
            System.Xml.XmlDocument docTemp = new System.Xml.XmlDocument();
            docTemp.LoadXml(str1);

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(Server.MapPath("XMLFile1.xml"));
            System.Xml.XmlNode node = doc.ImportNode(docTemp.DocumentElement.LastChild, true);
            doc.DocumentElement.AppendChild(node);
            doc.Save(Server.MapPath("XMLFile1.xml"));
            lblTips.Text = "创建新节点成功!";
        }
    }
}

 

Guess you like

Origin blog.csdn.net/modern358/article/details/114835172