xml增删改查

==============================XML数据岛即表格绑定xml

----------------------Table绑定XML_js实现翻页

文件夹8
Test.htm:
<!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>
    <title>在HTML页中显示XML数据</title>
    <script type="text/javascript">//js实现翻页
function movenext()
{

    x=test.recordset;
if (x.absoluteposition < x.recordcount)
{
    x.movenext();
}
}
function moveprevious()
{
    x=test.recordset;
if (x.absoluteposition > 1)
{
    x.moveprevious();
}
}
</script>
</head>
<body>
<xml src="test.xml" id="test"></xml>//手动引入xml
<table align="center" border="1" cellpadding="0" cellspacing="0">
        <tr>
            <td>
<table border="1" cellpadding="0" cellspacing="0" style="font-size:9pt; width:200px; text-align:center; color:white; background-color: #ff9933;" align="center">
<tr>
<td style="width:60px; height: 16px;">员工姓名</td>
<td style="width:60px; height: 16px;">员工薪水</td>
</tr></table>
<table datasrc="#test" border="1" cellpadding="0" cellspacing="0" style="font-size:9pt; width:200px; text-align:center; background-color: #ffffcc;" align="center">
<tr>
<td style="width:60px; height: 4px;"><span datafld="EName"/></td>
<td style="width:60px; height: 4px;"><span datafld="EMoney"/></td>
</tr>
</table>
            </td>
        </tr>
        <tr>
            <td>
    <table style="font-size:9pt; width:300px" align="center" border="1" cellpadding="0" cellspacing="0">
    <tr><td style="color: #ffffff; text-align: center; background-color: #ff9900;">查看员工详细信息</td></tr>
        <tr>

//td中:添加<span datasrc="#text" datafld="EName"></span>//EName:元素名称  "#text"加#
            <td style="background-color: #ffffcc; font-weight:bold;">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;员工姓名:
&nbsp;&nbsp;&nbsp;&nbsp;<span datasrc="#test" datafld="EName" style="color:Black;"></span>
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;员工性别:
&nbsp;&nbsp;&nbsp;&nbsp;<span datasrc="#test" datafld="ESex" style="color:Black;"></span>
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;员工年龄:
&nbsp;&nbsp;&nbsp;&nbsp;<span datasrc="#test" datafld="EAge" style="color:Black;"></span>
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;员工籍贯:
&nbsp;&nbsp;&nbsp;&nbsp;<span datasrc="#test" datafld="EPlace" style="color:Black;"></span>
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;员工薪水:
&nbsp;&nbsp;&nbsp;&nbsp;<span datasrc="#test" datafld="EMoney" style="color:Black;"></span>
            </td>
        </tr>
        <tr>
            <td style="background-color: #ffffcc; text-align:center;">
<input type="button" value="上一条"
οnclick="moveprevious()" style="font-size: 9pt" />
<input type="button" value="下一条"
οnclick="movenext()" style="font-size: 9pt" />
            </td>.
        </tr>
    </table>
            </td>
        </tr>
    </table>
</body>
</html>

text.xml:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <EmployeeID>1</EmployeeID>
    <EName>明日测试</EName>
    <ESex>男</ESex>
    <EAge>25</EAge>
    <EPlace>吉林省长春市</EPlace>
    <EMoney>5000</EMoney>
  </Table>
  <Table>
    <EmployeeID>2</EmployeeID>
    <EName>张三</EName>
    <ESex>男</ESex>
    <EAge>28</EAge>
    <EPlace>吉林省长春市</EPlace>
    <EMoney>3000</EMoney>
  </Table>
  <Table>
    <EmployeeID>3</EmployeeID>
    <EName>李四</EName>
    <ESex>女</ESex>
    <EAge>23</EAge>
    <EPlace>山西省长治市</EPlace>
    <EMoney>3000</EMoney>
  </Table>
  <Table>
    <EmployeeID>4</EmployeeID>
    <EName>王五</EName>
    <ESex>女</ESex>
    <EAge>26</EAge>
    <EPlace>山西省长治市</EPlace>
    <EMoney>2000</EMoney>
  </Table>
</NewDataSet>
-------------------------------------------XML数据岛

《正文》

··   lifei  考试卷.dot  mr  2  3  1601-01-01T00:00:00Z  2008-06-22T11:00:00Z  2008-06-22T11:00:00Z  2  177  1009  8  2  1239  9.2812      5.25 磅  7.8 磅  0  2                               XML数据岛技术

利用XML数据岛技术,可以有效的将XML文档内容的显示格式和数据分离,XML文档中的数据可以以数据岛的形式嵌入到HTML页面中。

HTML页面中链接XML文档的语法格式如下:

<xml id="value" src="XML文件名">

 示例

使用XML数据岛技术显示XML文档

本示例通过使用XML数据岛技术显示XML文档的内容,示例运行结果如图1所示。

 

图1  使用XML数据岛技术显示XML文档

程序主要代码如下所示。

XML文件test.xml主要代码如下:

<?xml version="1.0" encoding="utf-8" ?>

<PUBLICATION>

  <BOOK>

    <TITLE>C#数据库系统开发完全手册</TITLE>

    <PAGES>628</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>ASP.NET程序开发范例宝典</TITLE>

    <PAGES>820</PAGES>

  </BOOK>

</PUBLICATION>

HTML文件test.htm主要代码如下:

<xml id="xmlid" src="test.xml"></xml>

<table datasrc="#xmlid" style="font-size: 9pt; width: 240px; text-align: center" align="center" border="1" cellpadding= "0" cellspacing="0">

  <thead>

    <td height="25" style="width: 265px">书名</td>

    <td height="25" style="width: 79px">页码</td>

  </thead>

  <tr>

    <td height="25" style="width: 265px"><SPAN datafld="TITLE"></SPAN></td>

    <td height="25" style="width: 79px"><SPAN datafld="PAGES"></SPAN></td>

  </tr>

</table>

完整程序代码如下:

★   ★★★★test.xml XML文件完整程序代码★★★★★

<?xml version="1.0" encoding="utf-8" ?>

<PUBLICATION>

  <BOOK>

    <TITLE>C#数据库系统开发完全手册</TITLE>

    <PAGES>628</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>ASP.NET程序开发范例宝典</TITLE>

    <PAGES>820</PAGES>

  </BOOK>

</PUBLICATION>

 
=====================================显示XML文件========================

---------------------------------XML控件显示XML文档

XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("list.xml"));
        Xml1.Document = doc;

----------------------------------显示XML文件

《正文》

··   lifei  考试卷.dot  mr  2  7  1601-01-01T00:00:00Z  2008-06-22T11:00:00Z  2008-06-22T11:00:00Z  2  623  3554  29  7  4364  9.2812      5.25 磅  7.8 磅  0  2                                显示XML文件

本节通过使用XSL样式转换,将XML文件中的内容显示在Web页面中。实例运行结果如图1所示。

 

图1  显示XML文件

程序开发步骤如下所示。

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

(2)在该网站中添加一个test.xml文件和一个test.xsl文件,它们分别用来存储数据和执行XSL样式转换。

(3)程序主要代码。

test.xml文件主要代码如下:

<?xml version="1.0" encoding="utf-8" ?>

<PUBLICATION>

  <BOOK>

    <TITLE>C#数据库系统开发完全手册</TITLE>

    <PAGES>628</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>ASP.NET程序开发范例宝典</TITLE>

    <PAGES>820</PAGES>

  </BOOK>

</PUBLICATION>

test.xsl文件主要代码如下:

<table width="300" height="30" border="10" cellspacing="0" cellpadding="0">

     <tr align="center">

      <td>书名</td>

      <td>页码</td>

     </tr>

     <xsl:for-each select="PUBLICATION/BOOK">

      <tr align="center" height="30">

        <td>

          <xsl:value-of select="TITLE"/>

        </td>

        <td>

          <xsl:value-of select="PAGES"/>

        </td>

      </tr>

     </xsl:for-each>

     </table>

 注意:本章31.4和31.5节的实例中用到的XML文件和XSL文件,与本节中的test.xml文件和test.xsl文件相同,不再赘述。

Default.aspx页面中,首先定义一个XmlDocument类对象,并使用其Load方法加载指定的XML文件,然后定义一个XslTransform类对象,并使用该对象的Load方法加载指定的XSL文件,最后将Xml文件中的内容显示在Xml1控件中,并使用XslTransform类对象对XML文件中的内容进行样式转换。Default.aspx页面的Page_Load事件代码如下所示:

protected void Page_Load(object sender, EventArgs e)

   {

     XmlDocument doc = new XmlDocument();

     doc.Load(Server.MapPath("test.xml"));//加载xml


     XslTransform trans = new XslTransform();

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


     Xml1.Document = doc;//xml1控件显示xml

     Xml1.Transform = trans;//xml1控件通过xsl样式显示xml

   }

 注意:在Web页面中显示XML文件和使用XSL样式转换时,需要添加命名空间using System.Xml和using System.Xml.Xsl。

完整程序代码如下:

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

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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">

    <div>

    <table align="center" cellpadding="0" cellspacing="0" style="width: 339px; height: 172px" border="1" bordercolor="#66cccc">

            <tr>

                <td style="font-size: 9pt; text-align: center; height: 18px; background-color: #66cccc;">

                    显示XML文件</td>

            </tr>

            <tr>

                <td style="font-size: 9pt; text-align:center; background-color: #ccffff;">

                    <asp:Xml ID="Xml1" runat="server"></asp:Xml></td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

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

    }

}

★   ★★★★test.xml XML文件完整程序代码★★★★★

<?xml version="1.0" encoding="utf-8" ?>

<PUBLICATION>

  <BOOK>

    <TITLE>C#数据库系统开发完全手册</TITLE>

    <PAGES>628</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>ASP.NET程序开发范例宝典</TITLE>

    <PAGES>820</PAGES>

  </BOOK>

</PUBLICATION>

★   ★★★★test.xsl XML格式转换文件完整程序代码★★★★★

<?xml version="1.0" encoding="utf-8"?>

 

<xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

  <xsl:template match="/">

    <html>

      <body>

        <!--

        This is an XSLT template file. Fill in this area with the

        XSL elements which will transform your XML to XHTML.

    -->

        <table width="300" height="30" border="10" cellspacing="0" cellpadding="0">

        <tr align="center">

          <td>书名</td>

          <td>页码</td>

        </tr>

        <xsl:for-each select="PUBLICATION/BOOK">

          <tr align="center" height="30">

            <td>

              <xsl:value-of select="TITLE"/>

            </td>

            <td>

              <xsl:value-of select="PAGES"/>

            </td>

          </tr>

        </xsl:for-each>

        </table>

      </body>

    </html>

  </xsl:template>

</xsl:stylesheet>

 
===============================删除XML节点

-----------------------------XMLDocument删除节点

 

 

文件夹7
text.xml:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <EmployeeID>1</EmployeeID>
    <EName>明*日</EName>
    <ESex>男</ESex>
    <EAge>25</EAge>
    <EPlace>吉林省长春市</EPlace>
    <EMoney>5000</EMoney>
  </Table>
  <Table>
    <EmployeeID>2</EmployeeID>
    <EName>张*三</EName>
    <ESex>男</ESex>
    <EAge>28</EAge>
    <EPlace>吉林省长春市</EPlace>
    <EMoney>3000</EMoney>
  </Table>
  <Table>
    <EmployeeID>3</EmployeeID>
    <EName>李*四</EName>
    <ESex>女</ESex>
    <EAge>23</EAge>
    <EPlace>山西省长治市</EPlace>
    <EMoney>3000</EMoney>
  </Table>
  <Table>
    <EmployeeID>4</EmployeeID>
    <EName>王*五</EName>
    <ESex>女</ESex>
    <EAge>26</EAge>
    <EPlace>山西省长治市</EPlace>
    <EMoney>2000</EMoney>
  </Table>
</NewDataSet>
.cs:
protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("test.xml"));
        Xml1.Document = doc;//Xml1加载xml文档
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim() != "")
        {
            Label1.Visible = true;
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("test.xml"));//加载xml
            XmlNodeList nodes;
            XmlElement root = doc.DocumentElement;
            nodes = root.SelectNodes("descendant::Table[EName='" + TextBox1.Text.Trim() + "']");//获取与XPath匹配的节点集合进行遍历   此处textBox1.text输入EName名称
            foreach (XmlNode node in nodes)
            {
                root.RemoveChild(node);
            }
            doc.Save(Server.MapPath("test.xml"));//重新保存xml
            Xml2.Document = doc;//xml2加载删除后的xml
        }
        else
            Response.Write("<script>alert('请输入要删除的节点');location='javascript:history.go(-1)';</script>");
    }
htm:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_7_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">
    <div>
        <div>
            <table align="center" border="1" cellpadding="0" cellspacing="0" style="width: 245px">
                <tr>
                    <td style="font-size: 9pt; color: #ffffff; background-color: #ff9933">
                        原XML文件:</td>
                </tr>
                <tr>
                    <td style="font-size: 9pt; background-color: #ffffcc; text-align: center">
                        <asp:Xml ID="Xml1" runat="server"></asp:Xml></td>
                </tr>
                <tr>
                    <td style="font-size: 9pt; color: #ff0000; background-color: #ff9933">
                        <asp:Label ID="Label1" runat="server" ForeColor="White" Text="删除节点后:" Visible="False"
                            Width="78px"></asp:Label></td>
                </tr>
                <tr>
                    <td style="font-size: 9pt; background-color: #ffffcc; text-align: center">
                        <asp:Xml ID="Xml2" runat="server"></asp:Xml></td>
                </tr>
                <tr>
                    <td style="font-size: 9pt; color: #ffffff; background-color: #ff6600; text-align: center">
                        <asp:LinkButton ID="LinkButton1" runat="server" Font-Size="9pt" Font-Underline="False"
                            ForeColor="White" OnClick="LinkButton1_Click">删除</asp:LinkButton>
                        <asp:TextBox ID="TextBox1" runat="server" Font-Size="9pt" Width="31px"></asp:TextBox>节点</td>
                </tr>
            </table>
        </div>
   
    </div>
    </form>
</body>
</html>
---------------------------------------------------------------删除XML节点

图1  删除XML节点
 
程序开发步骤如下所示。

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

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

(3)程序主要代码。

当单击【删除】按钮时,程序首先判断TextBox1文本框中内容是否为空,如果不为空,则根据TextBox1文本框中内容在XML文件中找到对应节点,并通过XmlElement类的RemoveChild方法将该节点删除,否则,弹出“请输入要删除的节点”信息提示框,并将网页重新定向到该页面。【删除】按钮的Click事件代码如下:

protected void Button1_Click(object sender, EventArgs e)

   {//思想:1加载xml 2:遍历指定 的节点下的元素删除  3:重新保存回xml  4:xml1控件加载xml以显示结果

     if (TextBox1.Text.Trim() != "")

     {

       XmlDocument doc = new XmlDocument();

       doc.Load(Server.MapPath("test.xml"));//加载xml

       XmlNodeList nodes;

       XmlElement root = doc.DocumentElement;

       nodes = root.SelectNodes("descendant::BOOK[TITLE='" + TextBox1.Text.Trim() + "']");//遍历指定的节点下面的元素

       foreach (XmlNode node in nodes)

       {

          root.RemoveChild(node);

       }

       TextBox1.Text="";

       Response.Write("<script>alert('删除成功')</script>");

       doc.Save(Server.MapPath("test.xml"));//重新保存回xml

       XslTransform trans = new XslTransform();

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

       Xml1.Document = doc;//xml1加载xml以显示

       Xml1.Transform = trans;//xml1加载xsl,执行xsl样式

     }

     else

       Response.Write("<script>alert('请输入要删除的节点');location='javascript:history.go(-1)';</script>");//location:定位:javascript:history.go(-1):记录返回(后退一步)

   }

完整程序代码如下:

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

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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>删除XML节点</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <table align="center" border="1" cellpadding="0" cellspacing="0" style="width: 245px" bordercolor="#66cccc">

        <tr><td style="font-size: 9pt; background-color: #66cccc; text-align: center;">

            删除XML节点</td></tr>

            <tr>

                <td style="font-size: 9pt; text-align: center; background-color: #ccffff;">

                    <asp:Xml ID="Xml1" runat="server"></asp:Xml></td>

            </tr>

            <tr>

                <td style="text-align: center; font-size: 9pt; background-color: #ccffff;">

                    <asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="书名:"></asp:Label>

                    <asp:TextBox ID="TextBox1" runat="server" Font-Size="9pt" Width="92px"></asp:TextBox>

                    &nbsp;&nbsp;

                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="删除" /></td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

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

    {

        if (TextBox1.Text.Trim() != "")

        {

            XmlDocument doc = new XmlDocument();

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

            XmlNodeList nodes;

            XmlElement root = doc.DocumentElement;

            nodes = root.SelectNodes("descendant::BOOK[TITLE='" + TextBox1.Text.Trim() + "']");

            foreach (XmlNode node in nodes)

            {

                root.RemoveChild(node);

            }

            TextBox1.Text = "";

            Response.Write("<script>alert('删除成功')</script>");

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

            XslTransform trans = new XslTransform();

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

            Xml1.Document = doc;

            Xml1.Transform = trans;

        }

        else

        {

            Response.Write("<script>alert('请输入要删除的节点');</script>");

        }

 

    }

}

★   ★★★★test.xml XML文件完整程序代码★★★★★

<?xml version="1.0" encoding="utf-8"?>

<PUBLICATION>

  <BOOK>

    <TITLE>C#数据库系统开发完全手册</TITLE>

    <PAGES>628</PAGES>

  </BOOK>

</PUBLICATION>

★   ★★★★test.xsl XML文件转换文件完整程序代码★★★★★

<?xml version="1.0" encoding="utf-8"?>

 

<xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

  <xsl:template match="/">

    <html>

      <body>

        <!--

        This is an XSLT template file. Fill in this area with the

        XSL elements which will transform your XML to XHTML.

    -->

        <table width="300" height="30" border="10" cellspacing="0" cellpadding="0" style="font-size: 9pt;">

        <tr align="center">

          <td style="font-size: 9pt;">书名</td>

          <td style="font-size: 9pt;">页码</td>

        </tr>

        <xsl:for-each select="PUBLICATION/BOOK">

          <tr align="center" height="30">

            <td style="font-size: 9pt;">

              <xsl:value-of select="TITLE"/>

            </td>

            <td style="font-size: 9pt;">

              <xsl:value-of select="PAGES"/>

            </td>

          </tr>

        </xsl:for-each>

        </table>

      </body>

    </html>

  </xsl:template>

</xsl:stylesheet>

 
==================================添加XML节点

------------------------------------------添加节点_写入XML_读出XML

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       this.xmlfun();
    }

    //使用XmlDocument添加节点
    private void xmlfun()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("phone.xml"));

        //创建元素
        XmlElement node = doc.CreateElement("phone");
        XmlAttribute atr = doc.CreateAttribute("id");//元素的属性,属性可有可无
        atr.InnerText = "003";
        node.Attributes.Append(atr);

        //创建子节点
        XmlNode xnode = (XmlNode)doc.CreateElement("Name");
        xnode.InnerText = "testName";//文本
        node.AppendChild(xnode);文本添加到节点

        xnode = (XmlNode)doc.CreateElement("Number");
        xnode.InnerText = "119";
        node.AppendChild(xnode);

        xnode = (XmlNode)doc.CreateElement("City");
        xnode.InnerText = "cs";
        node.AppendChild(xnode);

        xnode = (XmlNode)doc.CreateElement("DateOfBirth");
        xnode.InnerText = "12/02/1978";
        node.AppendChild(xnode);

        doc.DocumentElement.InsertAfter(node, doc.DocumentElement.LastChild);

        doc.Save(Server.MapPath("phone.xml"));   //必须要存为同的文件

    }

    //使用XmlTextWriter写入XML方法     相当于写一个新的xml

    private void xmlwriter()
    {
        XmlTextWriter writer = new XmlTextWriter(Server.MapPath("phone4.xml"), null);
//phone4.xml为空的xml,写入运行后此phon4.xml中才有内容
        writer.Formatting = Formatting.Indented;  //缩进格式
        writer.Indentation = 4;

        writer.WriteStartDocument();

        writer.WriteStartElement("Person");

        writer.WriteStartAttribute("ID", null);
        writer.WriteString("004");
        writer.WriteEndAttribute();

        writer.WriteStartElement("Name");
        writer.WriteString("testWriterName");
        writer.WriteEndElement();

        writer.WriteStartElement("Number");
        writer.WriteString("88888");
        writer.WriteEndElement();

        writer.WriteStartElement("City");
        writer.WriteString("testWriterCity");
        writer.WriteEndElement();

        writer.Flush();
        writer.Close();

    }
    //使用XmlTextReader读取XML方法    相当于写一个新的xml

    private void xmlread()
    {
       // XmlTextReader r = new XmlTextReader("phone.xml");
        this.Label1.Text = string.Empty;
        XmlTextReader reader = new XmlTextReader(Server.MapPath("phone.xml"));
         while (reader.Read())
        {          
            if (reader.LocalName.Equals("Name") || reader.LocalName.Equals("Number"))
            {
                this.Label1.Text += reader.ReadString() + "/t";
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.xmlwriter();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.xmlread();
    }
}
.html:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_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">
    <div>  
     <asp:button ID="Button1" runat="server" text="添加节点" OnClick="Button1_Click" />
        <asp:button ID="Button2" runat="server" text="读取NAME" OnClick="Button2_Click" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
    </form>
</body>
</html>

phone.xml
<?xml version="1.0" encoding="utf-8"?>
<PhoneBook>
  <phone id="001">
    <Name>加菲尔德</Name>
    <Number>5555555</Number>
    <City>纽约</City>
    <DateOfBirth>26/10/1978</DateOfBirth>
  </phone>
  <phone id="002">
    <Name>迈克</Name>
    <Number>6666666</Number>
    <City>纽约</City>
    <DateOfBirth>12/02/1978</DateOfBirth>
  </phone>
  <phone id="003">
    <Name>testName</Name>
    <Number>119</Number>
    <City>cs</City>
    <DateOfBirth>12/02/1978</DateOfBirth>
  </phone>
  <phone id="003">
    <Name>testName</Name>
    <Number>119</Number>
    <City>cs</City>
    <DateOfBirth>12/02/1978</DateOfBirth>
  </phone>
  <phone id="003">
    <Name>testName</Name>
    <Number>119</Number>
    <City>cs</City>
    <DateOfBirth>12/02/1978</DateOfBirth>
  </phone>
  <phone id="003">
    <Name>testName</Name>
    <Number>119</Number>
    <City>cs</City>
    <DateOfBirth>12/02/1978</DateOfBirth>
  </phone>
</PhoneBook>
phone4.xml
<?xml version="1.0"?>
<Person ID="004">
    <Name>testWriterName</Name>
    <Number>88888</Number>
    <City>testWriterCity</City>
</Person>

-----------------------------------------添加XML节点


 

图1  添加XML节点

程序开发步骤如下所示。

(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" %>

 

<!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>

    <form id="form1" runat="server">

    <div>

     <table align="center" style="font-size: 9pt; text-align: center" border="1" cellpadding="0" cellspacing="0" bordercolor="#66cccc">

            <tr>

                <td style="font-size: 9pt; background-color: #66cccc;">

                    添加XML节点</td>

            </tr>

            <tr>

                <td style="font-size: 9pt; width: 300px; background-color: #ccffff;">

                    <asp:Xml ID="Xml1" runat="server"></asp:Xml></td>

            </tr>

            <tr>

                <td style="font-size: 9pt; width: 300px; background-color: #ccffff;">

                    <table border="0" cellpadding="0" cellspacing="0">

                        <tr>

                            <td>

                                书名:</td>

                            <td>

                                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>

                        </tr>

                        <tr>

                            <td>

                                页码:</td>

                            <td>

                                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>

                        </tr>

                    </table>

                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="添加" /></td>

            </tr>

        </table>

    </div>

    </form>

</body>

</html>

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

    {//思想:1/加载xml 2定义节点变量 创建元素,把元素文本添加到节点中  3重新保存回xml   4利用xml1控件绑定样式表(xsl)显示xml

        XmlDocument doc1 = new XmlDocument();

        doc1.Load(Server.MapPath("test.xml"));//加载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"));//重新保存回xml

        XslTransform trans = new XslTransform();

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

        Xml1.Document = doc1;

        Xml1.Transform = trans;//利用xml1控件绑定样式表(xsl)显示xml

        TextBox1.Text = "";

        TextBox2.Text = "";

    }

}

★   ★★★★test.xml XML文件完整程序代码★★★★★

<?xml version="1.0" encoding="gb2312"?>

<PUBLICATION>

  <BOOK>

    <TITLE>C#数据库系统开发范例宝典</TITLE>

    <PAGES>628</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>ASP.NET程序开发范例宝典</TITLE>

    <PAGES>820</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>ASP.NET数据库系统开发案例精选</TITLE>

    <PAGES>530</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>C#程序开发范例宝典</TITLE>

    <PAGES>560</PAGES>

  </BOOK>

  <BOOK>

    <TITLE>JSP完全手册</TITLE>

    <PAGES>10000</PAGES>

  </BOOK>

</PUBLICATION>

★   ★★★★test.xsl XML格式转换文件完整程序代码★★★★★

<?xml version="1.0" encoding="utf-8"?>

 

<xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 

  <xsl:template match="/">

    <html>

      <body>

        <!--

        This is an XSLT template file. Fill in this area with the

        XSL elements which will transform your XML to XHTML.

    -->

        <table width="300" height="30" border="10" cellspacing="0" cellpadding="0">

        <tr align="center">

          <td>书名</td>

          <td>页码</td>

        </tr>

        <xsl:for-each select="PUBLICATION/BOOK">

          <tr align="center" height="30">

            <td>

              <xsl:value-of select="TITLE"/>

            </td>

            <td>

              <xsl:value-of select="PAGES"/>

            </td>

          </tr>

        </xsl:for-each>

        </table>

      </body>

    </html>

  </xsl:template>

</xsl:stylesheet>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值