/NewsSystem/ ├── index.html (Frameset 版面) ├── title.aspx (網站標題列) ├── news.aspx (新聞標題列表,繫結 contents.xml) ├── main.aspx (新聞內容頁,讀取對應 .xml) ├── manage.aspx (後台管理) ├── contents.xml (新聞索引) ├── hello.xml (新聞內容範例) ├── test.xml ├── first.xml └── dddd.xml
<html><head><title>簡易新聞系統</title></head>
<frameset border="0" bordercolor="#6699cc" framespacing="3" rows="85,*">
<frame name="title" noresize scrolling="no" src="title.aspx" />
<frameset cols="110,*">
<frame name="dir" src="news.aspx" scrolling="auto" style="border-right:1px solid #99ccff;" />
<frame name="main" src="main.aspx?name=hello" scrolling="yes" style="border-left:2px groove #99ccff;" />
</frameset>
</frameset>
</html>
<?xml version="1.0" encoding="GB2312"?>
<topiclist type="Dreamsite News">
<topic><title>歡迎進入新聞網!</title><href>main.aspx?name=hello</href></topic>
<topic><title>測試新聞</title><href>main.aspx?name=test</href></topic>
<topic><title>第一條新聞</title><href>main.aspx?name=first</href></topic>
<topic><title>第二條新聞</title><href>main.aspx?name=dddd</href></topic>
</topiclist>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<html><body style="font-size:12px;padding:5px;">
<%
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("contents.xml"));
XmlNodeList topics = doc.SelectNodes("topiclist/topic");
foreach (XmlNode topic in topics)
{
string title = topic.SelectSingleNode("title").InnerText;
string href = topic.SelectSingleNode("href").InnerText;
Response.Write($"<div style='margin:5px 0;'><a href='{href}' target='main'>{title}</a></div>");
}
%>
</body></html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<html><body style="padding:10px;font-size:13px;">
<%
string name = Request.QueryString["name"] ?? "hello";
string file = Server.MapPath(name + ".xml");
if (System.IO.File.Exists(file))
{
XmlDocument doc = new XmlDocument();
doc.Load(file);
string title = doc.SelectSingleNode("document/title").InnerText;
string author = doc.SelectSingleNode("document/author").InnerText;
string content = doc.SelectSingleNode("document/content").InnerText;
Response.Write($"<h3>{title}</h3><p>作者: {author}</p><div>{content}</div>");
}
else
{
Response.Write("<p>新聞不存在</p>");
}
%>
</body></html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
protected void btnSave_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim(); // 檔名 (不含副檔名)
string title = txtTitle.Text.Trim();
string author = txtAuthor.Text.Trim();
string content = txtContent.Text; // 含 HTML
// 1. 建立新聞內容 XML
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("document");
root.AppendChild(CreateElement(doc, "title", title));
root.AppendChild(CreateElement(doc, "author", author));
root.AppendChild(CreateElement(doc, "content", content));
doc.AppendChild(root);
doc.Save(Server.MapPath(name + ".xml"));
// 2. 更新 contents.xml 索引
XmlDocument idx = new XmlDocument();
idx.Load(Server.MapPath("contents.xml"));
XmlElement topic = idx.CreateElement("topic");
topic.AppendChild(CreateElement(idx, "title", title));
topic.AppendChild(CreateElement(idx, "href", $"main.aspx?name={name}"));
idx.DocumentElement.PrependChild(topic); // 新增置頂
idx.Save(Server.MapPath("contents.xml"));
lblMsg.Text = "發布成功!";
}
private XmlElement CreateElement(XmlDocument doc, string name, string innerText)
{
XmlElement el = doc.CreateElement(name);
el.InnerText = innerText;
return el;
}
</script>
<html><body>
<form runat="server">
<h3>新聞發布後台</h3>
檔名: <asp:TextBox ID="txtName" runat="server" /> (如 hello, test)<br />
標題: <asp:TextBox ID="txtTitle" runat="server" Width="300px" /><br />
作者: <asp:TextBox ID="txtAuthor" runat="server" /><br />
內容: <br /><asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine" Rows="10" Columns="60" /><br />
<asp:Button ID="btnSave" runat="server" Text="發布" OnClick="btnSave_Click" />
<asp:Label ID="lblMsg" runat="server" ForeColor="Green" />
</form>
</body></html>
| 優點 | 缺點 |
|---|---|
| 零資料庫部署簡單 | XML 檔案鎖定、併發寫入效能差 |
| 結構簡單易懂 | 無全文搜尋、分類、標籤功能 |
| 適合靜態內容為主 | Frameset 已廢棄,SEO 不友善 |
此範例展示 ASP.NET 快速建立免資料庫新聞系統的核心技術:XML 儲存、Frameset 版面、DataBind 顯示、XmlDocument 讀寫。雖架構簡陋,但適合學習 ASP.NET 基礎操作與 XML 應用。實務專案建議升級至資料庫儲存與現代版面架構。