用ASP.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>
这是一个很简单的XML文件,它的作用是用来显示新闻的列表。
hello.XML
<?XML version="1.0" encoding="GB2312"?>
<document>
<title>ASPcool news!</title>
<abstract>test news</abstract>
<author>feiying</author>
<content>
<paragraph>The firet test</paragraph>
</content>
</document>
这个文件是用来显示新闻的内容,其中各个意思大家一看就明白,我就不在这儿多说了。
下面给大家看新闻列表显示的页面。
news.ASPx
<%@ 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);