DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 用ASP.NET和XML做的新聞系統
用ASP.NET和XML做的新聞系統
編輯:XML詳解     
NT size=3> 這裡我就用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);
return writer.ToString();
}
</script>
</head>
<body>
<%=xslt()%>
<p align="center">


該程序由<a href="http://www.webjx.com">www.webjx.com</a>設計制作.

</p>
</body>
</Html>


  這個頁面完成了從XML通過xslt轉化成Html文件,也使我對於xslt有了進一步的認識。

  下面是新聞內容顯示的頁面:

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="http://www.webjx.com">www.webjx.com</a>設計制作</p>
</body>
</Html>


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

  最後,大家來看一下最負責的一個頁面,這個頁面的作用就是用來建立新的XML數據。

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("文件名已經存在,請重選文件名。");
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格式的內容。
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 hava input the article!");
TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
TextBox4.Text="";
TextBox5.Text="";
}
//向目錄文件中寫數據
}
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酷技術資訊網網站內容發布系統
</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">
內容:
</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>

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