DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> XML學習教程 >> XML詳解
用 ASP.NET 與 XML 打造一個新聞發布系統
編輯:XML詳解     

不用資料庫,改用 XML 當資料來源,也能做出一個堪用的新聞發布系統。以下示範一組最小的檔案組合,希望能拋磚引玉,讓更多人實際接觸這些技術。

一、資料層:兩份 XML 檔

contents.xml——存放新聞清單,每筆包含標題與連結:

<?xml version="1.0" encoding="GB2312"?>
<topiclist type="ASPCool News">
  <topic>
    <title>ASPcool news!</title>
    <href>main.aspx?name=hello</href>
  </topic>
  <topic>
    <title>Resolve a problem</title>
    <href>main.aspx?name=test</href>
  </topic>
</topiclist>

hello.xml——存放單篇新聞的內容,結構一目了然:

<?xml version="1.0" encoding="GB2312"?>
<document>
  <title>ASPcool news!</title>
  <abstract>test news</abstract>
  <author>feiying</author>
  <content>
    <paragraph>The first test</paragraph>
  </content>
</document>

二、列表頁:news.aspx

這個頁面的工作,是把 contents.xml 透過 XSLT 轉成 HTML:

<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Page Language="C#" Debug="true" codepage="936" %>
<html>
<head>
<script language="c#" runat="server">
public string xslt()
{
    StringWriter writer = new StringWriter();

    // 載入 XML
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(Server.MapPath("contents.xml"));

    // 載入 XSL
    XslTransform xsldoc = new XslTransform();
    xsldoc.Load(Server.MapPath("news.xsl"));

    // 轉換成 HTML
    XmlNodeNavigator nav = new XmlNodeNavigator(xmldoc);
    xsldoc.Transform(nav, null, writer);
    return writer.ToString();
}
</script>
</head>
<body>
<%= xslt() %>
</body>
</html>

三、內容頁:main.aspx

邏輯與列表頁完全相同,只是改成依網址參數決定要載入哪一份 XML:

XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath(Request["name"] + ".xml"));

XslTransform xsldoc = new XslTransform();
xsldoc.Load(Server.MapPath("main.xsl"));

XmlNodeNavigator nav = new XmlNodeNavigator(xmldoc);
xsldoc.Transform(nav, null, writer);

Request["name"] 抓到的值會直接拼接成檔名,因此 main.aspx?name=hello 就會去讀 hello.xml。這裡要提醒一個安全問題:正式環境務必對參數做白名單驗證,否則等於把任意檔案讀取的入口敞開在網路上。

四、管理頁:manage.aspx

這是最有挑戰性的一頁,負責建立新的 XML 資料。流程分兩半:先把新主題附加到 contents.xml 的清單,再把完整內容寫成一份新的 XML 檔。

public void Button1_Click(object sender, System.EventArgs e)
{
    // 檔名檢查,避免覆寫既有檔案
    if (File.Exists(Server.MapPath(TextBox1.Text + ".xml")))
    {
        Response.Write("檔名已經存在,請重選檔名。");
        Response.End();
        return;
    }

    // 1. 把新主題插入清單
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(Server.MapPath("contents.xml"));

    string insStr = "<topic><title>" + TextBox2.Text +
                    "</title><href>main.aspx?name=" + TextBox1.Text +
                    "</href></topic>";

    XmlDocumentFragment docFrag = xmldoc.CreateDocumentFragment();
    docFrag.InnerXml = insStr;

    XmlNode currNode = xmldoc.DocumentElement;
    currNode.InsertAfter(docFrag, currNode.LastChild);
    xmldoc.Save(Server.MapPath("contents.xml"));

    // 2. 把多行輸入轉成符合 XML 的段落結構
    string xmlfile = TextBox5.Text;
    xmlfile = xmlfile.Replace("&", "&amp;");
    xmlfile = xmlfile.Replace("<", "&lt;");
    xmlfile = xmlfile.Replace(">", "&gt;");
    xmlfile = xmlfile.Replace("\"", "&quot;");
    xmlfile = xmlfile.Replace("\n", "</paragraph><paragraph>");

    // 3. 組出完整文件並存檔
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<?xml version=\"1.0\" encoding=\"GB2312\"?>"
        + "<document><title>" + TextBox2.Text + "</title>"
        + "<abstract>" + TextBox4.Text + "</abstract>"
        + "<author>" + TextBox3.Text + "</author>"
        + "<content><paragraph>" + xmlfile +
          "</paragraph></content></document>");

    doc.Save(Server.MapPath(TextBox1.Text + ".xml"));

    // 清空表單
    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox3.Text = "";
    TextBox4.Text = "";
    TextBox5.Text = "";
}

前端表單有五個欄位:檔名、文章名稱、作者、摘要、內容(多行文字方塊),加上「提交」與「重置」兩個按鈕,用 ASP:textbox 與 ASP:label 這類伺服器端控制項搭配絕對定位排版即可。

這個架構的取捨

用 XML 當資料源的優點很明確:資料與呈現徹底分離,同一份 XML 換一張 XSLT 就能換一種版面,改版不必動資料;資料檔是純文字,攜帶與版本控制都方便。

但缺點也同樣明確。第一是併發與一致性——XML 檔沒有交易機制,兩個使用者同時發布,很可能互相覆寫,這在真實的新聞系統中是致命問題。第二是查詢能力,想找「某位作者的所有文章」只能整份載入後自行遍歷。第三是轉義風險,上面那段 Replace 處理順序很關鍵,必須先換 & 再換其他符號,否則會產生二次轉義;即便如此,仍不如直接改用 XmlDocument 逐節點組裝來得安全。

因此這套做法適合流量不大、內容變動頻率低、且需要多版面的站台。一旦進入多人協作、高查詢量的情境,就該把資料層換成資料庫,並保留 XML 作為交換格式或輸出格式。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved