这是一个写XML的例子,如果App_Data中没有NewBook.xml则创建一个,然后把下图中内容保存到NewBook.xml
1.xml文件
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
2.xmlReadWriteOperation.aspx
<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlReadWriteOperation.aspx.cs" Inherits="xmlReadWriteOperation" %>
<!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>XML读写综合例子</title>
</head>
<body bgcolor="buttonface">
<form id="form1" runat="server">
<div>
<table border=1>
<tr>
<td colspan="2" >
<b>Book Details:</b>
</td>
</tr>
<tr>
<td >
Genre:
</td>
<td>
<asp:TextBox ID="txtGenre" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td >
Title:
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Price:
</td>
<td>
<asp:TextBox ID="txtPrice" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button Text="Save" runat="server" ID="btnSave" OnClick="btnSave_Click"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label runat="server" ID="lblResult"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<!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>XML读写综合例子</title>
</head>
<body bgcolor="buttonface">
<form id="form1" runat="server">
<div>
<table border=1>
<tr>
<td colspan="2" >
<b>Book Details:</b>
</td>
</tr>
<tr>
<td >
Genre:
</td>
<td>
<asp:TextBox ID="txtGenre" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td >
Title:
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
Price:
</td>
<td>
<asp:TextBox ID="txtPrice" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button Text="Save" runat="server" ID="btnSave" OnClick="btnSave_Click"/>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label runat="server" ID="lblResult"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
3.xmlReadWriteOperation.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
/**//// <summary>
/// 功能:搜集页面上信息存储到xml中
/// 编码:邓古谚 flyin2006
/// 时间:2006.9.15
/// </summary>
public partial class xmlReadWriteOperation : System.Web.UI.Page
...{
Page_Load()#region Page_Load()
protected void Page_Load(object sender, EventArgs e)
...{
//注意:那个NewBooks.xml 里面的根Element是bookstore
//下面是几个未具体写例子的常用方法
//1. 节点修改:
//node.FirstChild.Value = "";
//2. 节点删除:
//node.RemoveChild(node.FirstChild);
//3. 事件用法:
// XmlDocument doc = new XmlDocument();
// doc.NodeInserted += new XmlNodeChangedEventHandler(
// NodeInsertedHandler);
// void NodeInsertedHandler(Object src, XmlNodeChangedEventArgs args)
// {
// Response.Write(“Node “ + args.Node.Name + “ inserted”);
// }
//4. 一次插入多条数据
//XmlDocumentFragment docFragment = doc.CreateDocumentFragment();
//docFragment.InnerXml = “<book genre=’novel’>...</book>”;
//parentNode.AppendChild(docFragment);
}
#endregion
包括读,写xml的操作的综合例子#region 包括读,写xml的操作的综合例子
protected void btnSave_Click(object sender, EventArgs e)
...{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/NewBooks.xml";
XmlDocument newBookDocument = new XmlDocument();
//检查文件是否存在
if (System.IO.File.Exists(xmlPath))
...{
newBookDocument.Load(xmlPath);
XmlNode bookNode = CreateBookNode(newBookDocument);
XmlNode bookStoreNode = newBookDocument.SelectSingleNode("/bookstore");
bookStoreNode.AppendChild(bookNode);
lblResult.Text = "Xml Document 更新成功";
}
else
...{
XmlNode nodeDeclartion = newBookDocument.CreateXmlDeclaration("1.0", "", "");
newBookDocument.AppendChild(nodeDeclartion);
XmlNode nodeComment = newBookDocument.CreateComment("这是一个综合的xml操作例子,包括读写");
newBookDocument.AppendChild(nodeComment);
XmlNode bookStoreNode = newBookDocument.CreateElement("bookstore");
XmlNode bookNode = CreateBookNode(newBookDocument);
bookStoreNode.AppendChild(bookNode);
newBookDocument.AppendChild(bookStoreNode);
lblResult.Text = "Xml Document 创建成功";
}
newBookDocument.Save(xmlPath);
}
#endregion
函数功能:在某个XmlDocument上增加一个书节点#region 函数功能:在某个XmlDocument上增加一个书节点
protected XmlNode CreateBookNode(XmlDocument NewBookDocument)
...{
//先建立一个书节点
XmlNode bookNode = NewBookDocument.CreateElement("book");
XmlAttribute bookAttribute = NewBookDocument.CreateAttribute("genre");
bookAttribute.Value = txtGenre.Text;
using System.Data;
using System.Configuration;
using System.Collections;
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;
/**//// <summary>
/// 功能:搜集页面上信息存储到xml中
/// 编码:邓古谚 flyin2006
/// 时间:2006.9.15
/// </summary>
public partial class xmlReadWriteOperation : System.Web.UI.Page
...{
Page_Load()#region Page_Load()
protected void Page_Load(object sender, EventArgs e)
...{
//注意:那个NewBooks.xml 里面的根Element是bookstore
//下面是几个未具体写例子的常用方法
//1. 节点修改:
//node.FirstChild.Value = "";
//2. 节点删除:
//node.RemoveChild(node.FirstChild);
//3. 事件用法:
// XmlDocument doc = new XmlDocument();
// doc.NodeInserted += new XmlNodeChangedEventHandler(
// NodeInsertedHandler);
// void NodeInsertedHandler(Object src, XmlNodeChangedEventArgs args)
// {
// Response.Write(“Node “ + args.Node.Name + “ inserted”);
// }
//4. 一次插入多条数据
//XmlDocumentFragment docFragment = doc.CreateDocumentFragment();
//docFragment.InnerXml = “<book genre=’novel’>...</book>”;
//parentNode.AppendChild(docFragment);
}
#endregion
包括读,写xml的操作的综合例子#region 包括读,写xml的操作的综合例子
protected void btnSave_Click(object sender, EventArgs e)
...{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/NewBooks.xml";
XmlDocument newBookDocument = new XmlDocument();
//检查文件是否存在
if (System.IO.File.Exists(xmlPath))
...{
newBookDocument.Load(xmlPath);
XmlNode bookNode = CreateBookNode(newBookDocument);
XmlNode bookStoreNode = newBookDocument.SelectSingleNode("/bookstore");
bookStoreNode.AppendChild(bookNode);
lblResult.Text = "Xml Document 更新成功";
}
else
...{
XmlNode nodeDeclartion = newBookDocument.CreateXmlDeclaration("1.0", "", "");
newBookDocument.AppendChild(nodeDeclartion);
XmlNode nodeComment = newBookDocument.CreateComment("这是一个综合的xml操作例子,包括读写");
newBookDocument.AppendChild(nodeComment);
XmlNode bookStoreNode = newBookDocument.CreateElement("bookstore");
XmlNode bookNode = CreateBookNode(newBookDocument);
bookStoreNode.AppendChild(bookNode);
newBookDocument.AppendChild(bookStoreNode);
lblResult.Text = "Xml Document 创建成功";
}
newBookDocument.Save(xmlPath);
}
#endregion
函数功能:在某个XmlDocument上增加一个书节点#region 函数功能:在某个XmlDocument上增加一个书节点
protected XmlNode CreateBookNode(XmlDocument NewBookDocument)
...{
//先建立一个书节点
XmlNode bookNode = NewBookDocument.CreateElement("book");
XmlAttribute bookAttribute = NewBookDocument.CreateAttribute("genre");
bookAttribute.Value = txtGenre.Text;