使用X M L建簡單留言板全程演練

X M L代表扩展标记语言( Extensible Markup Language),是由World Wide Web Consortium

W 3 C)的X M L工作组定义的。这个工作组是这样描述该语言的:

“扩展标记语言( X M L)是S G M L的子集,其目标是允许普通的S G M L在We b上以目前

H T M L的方式被服务、接收和处理。X M L被设计成易于实现,且可在S G M L和H T M L之间互相操作。”

看眷這段由X M L工作组在1 9 9 8年2月發布的<<X M L规范1 . 0版本>>,你可能就會一關霧水啦﹐這其中有多個朮語﹐似乎有點矛盾﹐似乎有點……總覺得有點拗口什么的:

我们已经有了H T M L,而且它已发展成一种似乎可以满足任何需要的语言,为什么我们还需要在We b上采用一种全新的语言呢? X M L有什么新特性和不同之处?它有什么特有的优点和长处呢?它与H T M L有什么关系?它是要替换还是增强H T M L?最后,这个以X M L作为其子集的S G M L是什么,为什么我们不就用S G M L来创建We b页呢?

如果要解釋這些內容﹐大概也可寫一本圖書了﹐只好請各位參考相關XML文件了。J

我只描述一下﹐本人對此的理解﹕

1﹑X M L的定义只是由框架语法组成。当创建一个X M L文档时,不必使用有限的预定义元素集,

而是创建自己的元素,并赋予任意你喜欢的名称—这就是扩展标记语言( Extensible Markup

L a n g u a g e)中术语“扩展”的意义。

2﹑使用X M L描述數據庫﹐比使用专用格式(例如A c c e s s的. m d b和d B a s e的. d b f格式)多了两个好处: X M L是可供人阅读的,而且它基于一种公共的开发标准。

有監于此﹐本人本次以一個XML作為數據庫﹐建一個簡單的BBS留言本。

FOLLOW ME!

首先申明一下﹐我們使用的是Microsoft的VisualStudio.Net2003的IDE.如果沒有該工具的朋友﹐很遺憾您將失去很多便捷的方法的﹐嘿嘿﹐這決不是為MS做廣告的J

言歸正傳 ﹕

1﹑打開VisualStudio 2003.NET, 建立一個新的ASP.NET WEB 應用程序項目,刪除默認的WebForm1.aspx.然后新一個名名﹕MessageList.aspx的WebForm頁。切換至HTML模式﹐輸入以下HTML代碼:

<%@ Page language="c#" Codebehind="MessageList.aspx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageList" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

        <HEAD>

                <title>MessageList</title>

                <meta name="GENERATOR" Content="Microsoft Visual Studio 7.1">

                <meta name="CODE_LANGUAGE" Content="C#">

                <meta name="vs_defaultClientScript" content="JavaScript">

                <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

                <style>

                .body { FONT-SIZE: 12px }

                .td1 { FONT-SIZE: 12px }

                </style>

        </HEAD>

        <body class="body">

                <form id="MessageList" method="post" runat="server">

                        <P align="right">

                                <asp:HyperLink id="GrowHyperLink1" runat="server" Text="[留言]" NavigateUrl="MessageAdd.aspx"></asp:HyperLink>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                        </P>

                        <P align="center"><FONT face="宋體"></FONT><FONT face="宋體"></FONT><BR>

                                <asp:DataGrid id="DataList1" runat="server" AutoGenerateColumns="False" ShowHeader="False" PageSize="15" BorderStyle="None" BorderWidth="0px" CellPadding="4" Font-Size="12px">

                                        <Columns>

                                                <asp:TemplateColumn>

                                                        <ItemTemplate>

                                                                <TABLE id="Table7" borderColor="#efefef" cellSpacing="0" cellPadding="4" width="749" bgColor="#efefef" border="1">

                                                                        <TR class="td1" bgColor="#fafafa">

                                                                                <TD style="WIDTH: 200px" bgColor="#ffffff"><STRONG>姓名:</STRONG>

                                                                                        <asp:Label id=Label5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserName").ToString() %>'>

                                                                                        </asp:Label>&nbsp;&nbsp;

                                                                                </TD>

                                                                                <TD style="WIDTH: 150px" bgColor="#ffffff"><STRONG><FONT face="宋體">時間</FONT></STRONG><STRONG><FONT face="宋體">:</FONT></STRONG>

                                                                                        <asp:Label id=Label4 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SendTime").ToString() %>'>

                                                                                        </asp:Label></TD>

                                                                                <TD bgColor="#ffffff"><STRONG>E-Mail</STRONG><STRONG>:

                                                                                                <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%# "mailto:"+DataBinder.Eval(Container, "DataItem.UserEmail").ToString() %>' Text='<%# DataBinder.Eval(Container, "DataItem.UserEmail").ToString() %>'>

                                                                                                </asp:HyperLink></STRONG></TD>

                                                                        </TR>

                                                                        <TR class="td1">

                                                                                <TD bgColor="#ffffff" colSpan="3">

                                                                                        <asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Content").ToString() %>'>

                                                                                        </asp:Label></TD>

                                                                        </TR>

                                                                </TABLE>

                                                        </ItemTemplate>

                                                </asp:TemplateColumn>

                                        </Columns>

                                        <PagerStyle Visible="False"></PagerStyle>

                                </asp:DataGrid>

                        </P>

                </form>

        </body>

</HTML>

2﹑鼠標右鍵﹐選擇檢視程式碼﹐進入代碼編輯器中﹐然后在Page_Load 中輸入以下程式碼﹕

                private void Page_Load(object sender, System.EventArgs e)

                {

                        FileStream stream = new FileStream(Server.MapPath("db//db.xml"),FileMode.Open);

                        try

                        {

                                XmlDataDocument doc = new XmlDataDocument();

                                doc.DataSet.ReadXml(new StreamReader(stream));

                                if(doc.DataSet.Tables.Count != 0 && doc.DataSet.Tables[0].Rows.Count!=0)

                                {

                                        DataList1.DataSource = doc.DataSet.Tables[0].DefaultView ;

                                        DataList1.DataBind();

                                }

                                stream.Close();

                        }

                        catch(Exception ding)

                        {

                                Response.Write(ding.Message);

                        }

                        finally

                        {

                                stream.Close();

                        }

 

3﹑再新加一個名為﹕MessageAdd.aspx的Web Form頁面。輸入HTML代碼如下

<%@ Page language="c#" Codebehind="MessageAdd.aspx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageAdd" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

    <HEAD>

        <title>MessageAdd</title>

        <meta content="Microsoft Visual Studio 7.1" name="GENERATOR">

        <meta content="C#" name="CODE_LANGUAGE">

        <meta content="JavaScript" name="vs_defaultClientScript">

        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

        <style>.Txt1 { FONT-SIZE: 12px }

        </style>

    </HEAD>

    <body>

        <form id="MessageAdd" method="post" runat="server">

            <FONT face="宋體">

                <P><FONT face="新細明體"></FONT>&nbsp;</P>

                <P align="center">

                    <TABLE class="Txt1" id="Table1" cellSpacing="1" cellPadding="4" width="710" bgColor="#efefef"

                        border="0" style="WIDTH: 710px; HEIGHT: 245px">

                        <TR>

                            <TD bgColor="#e8dabd">您的大名</TD>

                            <TD style="WIDTH: 508px" bgColor="#ffffff"><asp:TextBox id="TextBoxUName" runat="server" CssClass="TextAreaClass" MaxLength="10" Width="519px"

                                    HtmlEncode="False" LeaveFormat="False" LeaveSQL="False"></asp:TextBox></TD>

                            <TD bgColor="#ffffff" colSpan="1" rowSpan="1">

                                <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"

                                    ControlToValidate="TextBoxUName">請輸入您的大名!</asp:RequiredFieldValidator></TD>

                        </TR>

                        <TR>

                            <TD bgColor="#e8dabd" colSpan="1" rowSpan="1">E-Mail</TD>

                            <TD style="WIDTH: 505px" bgColor="#ffffff">

                                <P><asp:TextBox id="TextBoxMail" runat="server" CssClass="TextAreaClass" MaxLength="30" Width="519px"

                                        HtmlEncode="False" LeaveFormat="False" LeaveSQL="False"></asp:TextBox></P>

                            </TD>

                            <TD bgColor="#ffffff" colSpan="1" rowSpan="1">

                                <asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator"

                                    ControlToValidate="TextBoxMail">E-Mail不能為空﹗</asp:RequiredFieldValidator><BR>

                                <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"

                                    ValidationExpression="/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*" ControlToValidate="TextBoxMail">E-Mail輸入有誤﹗</asp:RegularExpressionValidator>

                            </TD>

                        </TR>

                        <TR>

                            <TD bgColor="#e8dabd" colSpan="1" rowSpan="1">內容</TD>

                            <TD style="WIDTH: 505px" bgColor="#ffffff">

                                <P><asp:TextBox id="TextBoxTxt" runat="server" Width="519px" HtmlEncode="False" LeaveFormat="True"

                                        LeaveSQL="False" Height="160px" TextMode="MultiLine"></asp:TextBox></P>

                            </TD>

                            <TD bgColor="#ffffff" colSpan="1" rowSpan="1">

                                <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator"

                                    ControlToValidate="TextBoxTxt">留言內容不能為空﹗</asp:RequiredFieldValidator></TD>

                        </TR>

                    </TABLE>

                </P>

                <P align="center"><asp:button id="Button1" runat="server" CssClass="TextAreaClass" Text=" 提交 "></asp:button>&nbsp;&nbsp;&nbsp;&nbsp;

                    &nbsp; <INPUT class="TextAreaClass" οnclick="window.location.replace('MessageList.aspx')" type="button"

                        value=" 返回 "></P>

            </FONT>

        </form>

    </body>

</HTML>

 

4﹑切換至設計模式﹐雙擊提交按鈕﹐進入代碼編輯器﹐輸入以下代碼﹕

                private void Button1_Click(object sender, System.EventArgs e)

                {

                                XmlDocument doc = new XmlDocument();

                                doc.Load(MapPath("db//db.xml"));

                                XmlNode node = doc.CreateElement("Item");

 

                                XmlAttribute xmlattr = doc.CreateAttribute("UserName");

                                xmlattr.Value = XmlConvert.DecodeName(TextBoxUName.Text);

                                node.Attributes.Append(xmlattr);

 

                                xmlattr = doc.CreateAttribute("UserEmail");

                                xmlattr.Value = XmlConvert.DecodeName(TextBoxMail.Text);

                                node.Attributes.Append(xmlattr);

 

                                xmlattr = doc.CreateAttribute("Content");

                                xmlattr.Value = XmlConvert.DecodeName(TextBoxTxt.Text);

                                node.Attributes.Append(xmlattr);

 

                                xmlattr = doc.CreateAttribute("SendTime");

                                xmlattr.Value = XmlConvert.DecodeName(DateTime.Now.ToString());

                                node.Attributes.Append(xmlattr);

 

                                doc.ChildNodes[0].AppendChild(node);

                                doc.Save(MapPath("db//db.xml"));

                                Response.Redirect("MessageList.aspx");

                }      

 

5﹑講到這一步﹐似乎跟XML還沒有挂一點邊﹐別急﹐接下來我們就需要建立一個XML文件﹐以作為類似于數據庫的作用的。

新增一個文件夾﹐假設名為﹕db,然后在其后新增一個XML文件﹐名為db.xml﹐我前面說過﹕

当创建一个X M L文档时,不必使用有限的预定义元素集,而是创建自己的元素,并赋予任意你喜欢的名称所以我只在該文件中輸入了兩行代碼﹕

<Group>

</Group>

 

6﹑最后保存退出。

7﹑測試﹕在IE中輸入﹕http://localhost/XMLGuest/MessageList.aspx就可看到你所做的成果了。

怎么樣﹐簡單吧﹐如果需要源碼﹐請單擊這里下載。

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页