讀了不少 .NET 與 XML 的資料後,筆者深深被它們的魅力吸引。關於「.NET 好還是 XML 好」的爭論其實沒多大意義,因為 .NET 早已和 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>
這個頁面透過 XSLT 把 XML 轉換成 HTML:
<%@ Import Namespace="System"%>
<%@ Page Language="C#" Debug="true" codepage="936"%>
<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.XML" %>
<%@ Import Namespace="System.XML" %>
<%@ Import Namespace="System.XML.Xsl" %>
<Html>
<head>
<title>
</title>
<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"));
//把XML轉化成Html頁面
DocumentNavigator nav= new DocumentNavigator(xmldoc);
xsldoc.Transform(nav,null,writer);
return writer.ToString();
}
</script>
</head>
<body>
<%=xslt()%>
</body>
</Html>
功能與列表頁相同,只是改依 Request["name"] 參數載入對應的 XML 檔,並套用 main.xsl:
<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.XML" %>
<%@ Import Namespace="System.XML" %>
<%@ Import Namespace="System.XML.Xsl" %>
<script language="c#" runat="server">
public string xslt()
{
StringWriter writer = new StringWriter();
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(Server.MapPath(Request["name"] +".XML"));
XslTransform xsldoc = new XslTransform();
xsldoc.Load(Server.MapPath("main.xsl"));
DocumentNavigator nav= new DocumentNavigator(xmldoc);
xsldoc.Transform(nav,null,writer);
return writer.ToString();
}
</script>
這個頁面的作用是建立新的 XML 資料。它先判斷檔名是否已存在,若無則把新主題附加到 contents.xml,再把使用者輸入的內容轉成合法 XML 後寫入新檔。其中特別用 Replace 把 &、<、>、引號與換行等字元轉義,並把換行轉成 </paragraph><paragraph>,以符合 XML 格式:
public void Button1_Click(object sender, System.EventArgs e)
{
//判斷文件是否存在
if(File.Exists(Server.MapPath(TextBox1.Text +".XML")))
{
Response.Write("文件名已經存在,請重選文件名。");
Response.End() ;
}
else
{
XmlNode currNode;
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;
currNode = xmldoc.DocumentElement;
currNode.InsertAfter(docFrag, currNode.LastChild);
xmldoc.Save (Server.MapPath("contents.XML"));
//把TextBox5中的內容換成符合XML格式的資料
string xmlfile =TextBox5.Text.Replace("&","&");
xmlfile = xmlfile.Replace("<","<");
xmlfile = xmlfile.Replace(">",">");
xmlfile = xmlfile.Replace( @"""",""");
xmlfile = xmlfile.Replace("'","'");
xmlfile = xmlfile.Replace ("\n","</paragraph><paragraph>");
//把資料寫入新建的XML文件
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"));
Response.Write("You have input the article!");
}
}
表單部分則以 Asp:label 與 Asp:textbox 控制項,分別對應檔名、文章名稱、作者、摘要與內容五個欄位,並附上提交與重設按鈕。此程式在 .NET beta2 build 9148 下測試通過。