明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

用ASP.NET與XML做的新聞系統(tǒng)

[摘要]這里我就用xml代替數(shù)據(jù),寫一個(gè)新聞發(fā)布系統(tǒng),希望能夠起到拋磚引玉的作用,使更多的人能夠了解這些最新的技術(shù)。下面介紹這幾個(gè)文件。 contents.xml <?xml version="1.0" encoding="GB2312"?> <t...

  這里我就用xml代替數(shù)據(jù),寫一個(gè)新聞發(fā)布系統(tǒng),希望能夠起到拋磚引玉的作用,使更多的人能夠了解這些最新的技術(shù)。下面介紹這幾個(gè)文件。


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> 

  這是一個(gè)很簡(jiǎn)單的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> 

  這個(gè)文件是用來顯示新聞的內(nèi)容,其中各個(gè)意思大家一看就明白,我就不在這兒多說了。

  下面給大家看新聞列表顯示的頁面。


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對(duì)象
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(Server.MapPath("Contents.xml"));
//裝入xsl對(duì)象
XslTransform xsldoc = new XslTransform();
xsldoc.Load(Server.MapPath("news.xsl"));
//把xml轉(zhuǎn)化成html頁面
DocumentNavigator nav= new DocumentNavigator(xmldoc);
xsldoc.Transform(nav,null,writer);
return writer.ToString();
}
</script>
</head>
<body>
<%=xslt()%>
<p align="center">

  該程序由<a href="www.aspcool.comhttp://www.aspcool.com">www.aspcool.com</a>設(shè)計(jì)制作.


</p>
</body>
</html>

  這個(gè)頁面完成了從xml通過xslt轉(zhuǎn)化成html文件,也使我對(duì)于xslt有了進(jìn)一步的認(rèn)識(shí)。

  下面是新聞內(nèi)容顯示的頁面:


main.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();
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>
</head>
<body>
<%=xslt()%>
<p align="center">該程序由<a href="www.aspcool.comhttp://www.aspcool.com">www.aspcool.com</a>設(shè)計(jì)制作.</p>
</body>
</html> 

  這個(gè)功能和上面的一樣,我在這兒就不多說了。


  最后,大家來看一下最負(fù)責(zé)的一個(gè)頁面,這個(gè)頁面的作用就是用來建立新的xml數(shù)據(jù)。


manage.aspx
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<%@ Page Language="C#" Debug="true" codepage="936"%>
<%@ Import Namespace="System"%>
<HTML>
<HEAD>
<script language="C#" runat="server">
public void Button1_Click(object sender, System.EventArgs e)
{
//判斷文件是否存在
if(File.Exists(Server.MapPath(TextBox1.Text +".xml")))
{
Response.Write("文件名已經(jīng)存在,請(qǐng)重選文件名。");
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);
//save the output to a file
xmldoc.Save (Server.MapPath("contents.xml"));
//把TextBox5中的文件換成符合xml格式的內(nèi)容。
string xmlfile =TextBox5.Text.Replace("&","&");
xmlfile = xmlfile.Replace("<","<");
xmlfile = xmlfile.Replace(">",">");
xmlfile = xmlfile.Replace( @"""""",""");
xmlfile = xmlfile.Replace(""","&apos;");
xmlfile = xmlfile.Replace ("\n","</paragraph><paragraph>");
//把數(shù)據(jù)寫入新建的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 hava input the article!");
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
TextBox4.Text="";
TextBox5.Text="";
}
//向目錄文件中寫數(shù)據(jù)
}
public void Button2_Click(object sender, System.EventArgs e)
{}
</script>
<meta content="Internet Explorer 5.0" name=vs_targetSchema>
<meta content="Microsoft Visual Studio 7.0" name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form runat="server">
<FONT face=宋體>
<asp:label id=Label1 style="Z-INDEX: 100; LEFT: 230px; POSITION:
absolute; TOP: 27px" runat="server" Height="28px" Width="156px">
asp酷技術(shù)資訊網(wǎng)網(wǎng)站內(nèi)容發(fā)布系統(tǒng)
</asp:label>
<asp:label id=Label2 style="Z-INDEX: 101; LEFT: 110px; POSITION:
absolute; TOP: 68px" runat="server" Height="25px" Width="65px">
文件名:
</asp:label>
<asp:textbox id=TextBox1 style="Z-INDEX: 102; LEFT: 255px; POSITION:
absolute; TOP: 64px" runat="server" Height="33px" Width="178px" >
</asp:textbox>
<asp:label id=Label3 style="Z-INDEX: 103; LEFT: 108px; POSITION:
absolute; TOP: 126px" runat="server" Height="36px" Width="86px">
文章名稱:
</asp:label>
<asp:textbox id=TextBox2 style="Z-INDEX: 104; LEFT: 256px; POSITION:
absolute; TOP: 114px" runat="server" Height="37px" Width="177px">
</asp:textbox>
<asp:label id=Label4 style="Z-INDEX: 105; LEFT: 114px; POSITION:
absolute; TOP: 183px" runat="server" Height="31px" Width="89px">
作者:
</asp:label>
<asp:textbox id=TextBox3 style="Z-INDEX: 106; LEFT: 256px; POSITION:
absolute; TOP: 183px" runat="server" Height="36px" Width="179px">
</asp:textbox>
<asp:label id=Label5 style="Z-INDEX: 107; LEFT: 114px; POSITION:
absolute; TOP: 241px" runat="server" Height="51px" Width="81px">
摘要:
</asp:label>
<asp:textbox id=TextBox4 style="Z-INDEX: 108; LEFT: 256px; POSITION:
absolute; TOP: 245px" runat="server" Height="36px" Width="179px">
</asp:textbox>
<asp:label id=Label6 style="Z-INDEX: 109; LEFT: 116px; POSITION:
absolute; TOP: 315px" runat="server" Height="36px" Width="78px">
內(nèi)容:
</asp:label>
<asp:textbox id=TextBox5 style="Z-INDEX: 110; LEFT: 259px; POSITION:
absolute; TOP: 303px" runat="server" Height="95px" Width="252px"
textmode="MultiLine">
</asp:textbox>
</FONT>


<INPUT id=Button2 style="Z-INDEX: 113; LEFT: 343px; WIDTH: 40px;
POSITION: absolute; TOP: 430px; HEIGHT: 24px" type=button value=重置
name=Button2 runat="server" OnServerClick="Button2_Click" DESIGNTIMEDRAGDROP="59">
<br>
<br>
<div id=mess runat=server>
</div>
<br>
<input type="button" value="提交" OnServerClick="Button1_Click"
runat="server" ID="Button1" NAME="Button1" style="Z-INDEX: 112;
LEFT: 268px; POSITION: absolute; TOP: 430px">
</form>
</body>
</HTML>
 

  此程序在.net beta2 build 9148下測(cè)試通過。