All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
.NET,2.0,SqlDependency快速上手指南_[Asp.Net教程]
smarty技术学习,修改php模板的一点体会_[PHP教程]
ASP.NET代码控制页面部分元素隐藏与显示_[Asp.Net教程]
ASP.net,做的IP,访问限制_[Asp.Net教程]
Asp.net,Ajax,学习笔记2,UpdatePanel的使用(上)_[Asp.Net教程]
C#中二维数组的声明
在ASP.NET里得到网站的域名_[Asp.Net教程]
如何在,SQL,Server,2005,中使用,DBCC,SHRINKFILE,语句收缩事务日志文件_[SQL,Server教程]
利用继承来制作点击提交按钮后按钮变灰_[Asp.Net教程]
C#应用:用Split()方法提取字符_[Asp.Net教程]
Reading number is top 10 pictures
China's first snake village3
Azusa Yamamoto2
NeedWallpaper8
随便发几张图
Go to the national museum3
Sanya, hainan Haitian party feast promiscuity
The real super beauty1
The goddess of the single reason1
不知名的美女素颜照1
小学生考试又现神作--还有外国的
Download software ranking
Professional killers2 data package
Unix video tutorial10
SP3 for SQL2000
Sora aoi‘s film--Lust fan wall
Photoshop 8.0图象编辑软件
Jinling thirteen stock
Love the forty days
Boxer's Top ten classic battle4
Popkart Cracked versions Mobile phone games
Ashlynn Video1
qq published in(发表于) 2014/7/11 9:19:56 Edit(编辑)
C#中添加XML节点实例

C#中添加XML节点实例

C#中添加XML节点实例|方法

添加XML节点

本节通过一个实例介绍如何在程序中向XML文件添加节点。实例运行结果如图1所示。

程序开发步骤如下所示。

(1)新建一个网站,命名为31_02,其主页默认为Default.aspx。

(2)在Default.aspx页面中添加一个Xml控件,用来显示XML文件中的内容,然后添加两个TextBox控件和一个Button控件,分别用来输入要添加的内容和执行添加操作。

(3)程序主要代码。

当单击【添加】按钮时,首先生成XmlDocument类的一个实例对象和两个XmlNode类对象,然后调用XmlDocument对象的CreateElement方法生成新的元素,并给最底层元素赋值,最后使用AppendClild方法将新生成的元素添加到XML文件中,并在Xml1控件中显示出来。【添加】按钮的Click事件代码如下:

protected void Button1_Click(object sender, EventArgs e)

{

XmlDocument doc1 = new XmlDocument();

doc1.Load(Server.MapPath("test.xml"));

XmlNode newNode1;

XmlNode newNode2;

newNode1=doc1.CreateElement("BOOK");

newNode2 = doc1.CreateElement("TITLE");

newNode2.InnerText = TextBox1.Text;

newNode1.AppendChild(newNode2);

newNode2 = doc1.CreateElement("PAGES");

newNode2.InnerText = TextBox2.Text;

newNode1.AppendChild(newNode2);

doc1.DocumentElement.AppendChild(newNode1);

doc1.Save(Server.MapPath("test.xml"));

XslTransform trans = new XslTransform();

trans.Load(Server.MapPath("test.xsl"));

Xml1.Document = doc1;

Xml1.Transform = trans;

TextBox1.Text = "";

TextBox2.Text = "";

}

完整程序代码如下:

★ ★★★★Default.aspx页面设计文件完整程序代码★★★★★

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>







添加XML节点















添加XML节点























书名:













页码:


























★ ★★★★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;

using System.Xml.Xsl;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

XmlDocument doc = new XmlDocument();

doc.Load(Server.MapPath("test.xml"));

XslTransform trans = new XslTransform();

trans.Load(Server.MapPath("test.xsl"));

Xml1.Document = doc;

Xml1.Transform = trans;

}

protected void Button1_Click(object sender, EventArgs e)

{

XmlDocument doc1 = new XmlDocument();

doc1.Load(Server.MapPath("test.xml"));

XmlNode newNode1;

XmlNode newNode2;



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.