程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> 一個ASP.NET+XML留言本例子

一個ASP.NET+XML留言本例子

編輯:.NET實例教程
vIEwpost.ASPx--察看提交的留言
vIEwguestbook.ASPx--察看所有留言
Header.inc
guestpost.ASPx--留言表單及XML寫操作
Footer.inc
Guest.xml--XML數據

源代碼如下:

vIEwguestbook.ASPx
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Assembly Name="System.XML" %>
<%@ Import Namespace="System.XML" %>
<%@ Page Language="C<%-- Needed Assembiles --%>

<Html>
<head>
<title>Welcome to Saurabh's GuestBook.</title>
<script language="C //run the script when the Page is Loaded
public void Page_Load(Object sender, EventArgs e)
{
// an label , its use stated later
tryagain :

//the path to the XML file which will contain all the data
//modify this if you have any other file or directory mappings.
//modify this if you have been directed here from Step 2 of the ReadMe file.
string datafile = "db/guest.XML" ;

//try-Catch block to read from an XML file
try
{
//make an instance to the XMLDataDocument class
//this class can read from an XML file in and ordered format
XmlDataDocument datadoc = new XMLDataDocument();

// Infer the DataSet schema from the XML data and load the XML Data
datadoc.DataSet.ReadXML(new StreamReader(Server.MapPath(datafile)));

//Databind the first table in the Dataset to the Repeter
MyDataList.DataSource = datadoc.DataSet.Tables[0].DefaultVIEw;
MyDataList.DataBind();

//free up the XML file to be used by other programs
datadoc=null;


}
catch(IOException ed)
{
// Here I am for now trying to overcome a bug in my guestbook exapmle
//the Bug is that only one class can either read or write to my XML
// data file at a time.
//If the file is being used my some some other page (eg the guest book vIEwing page)
// then an IOException will be thrown
// So to handle such situtations what we do is that
// If an IOException is thrown the page goes again to the tryagain label
//and trIEs to write again into the XML file
//this goes on till finally the resource is freed and the XML file is written to.

goto tryagain ;
}
catch (Exception edd)
{
//catch any other exceptions that occur
errmess.Text="Cannot read from XML file because "+edd.ToString() ;
}



}


</script>
<LINK href="mystyle.css" type=text/CSS rel=stylesheet>

</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" rightmargin="0">
<!-- <ASP:label id="errmess" text="" center" class="newsbody">My Guestbook.</h3>
<ASP:Repeater id="MyDataList" runat="server">

<template name="headertemplate">

<table class="mainheads" width="100%" >
<tr itemtemplate">

<tr footertemplate">

</table>

</template>

</ASP:Repeater>

<!-- </body>
</Html>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved