熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用ASP.NET和XML做的新聞系統

2022-06-13   來源: .NET編程 

  這裡我就用xml代替數據寫一個新聞發布系統希望能夠起到拋磚引玉的作用使更多的人能夠了解這些最新的技術下面介紹這幾個文件

contentsxml
<?xml version= encoding=GB?>
<topiclist type=AspCool News
<topic>
<title>aspcool news!</title>
<href>mainaspx?name=hello</href>
</topic>
<topic>
<title>Resolve a problem</title>
<href>mainaspx?name=test</href>
</topic>
</topiclist> 

  這是一個很簡單的xml文件它的作用是用來顯示新聞的列表

helloxml
<?xml version= encoding=GB?>
<document>
<title>aspcool news!</title>
<abstract>test news</abstract>
<author>feiying</author>
<content>
<paragraph>The firet test</paragraph>
</content>
</document> 

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

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

newsaspx
<%@ Import Namespace=System%>
<%@ Page Language=C# Debug=true codepage=%>
<%@ Import Namespace=SystemIO %>
<%@ Assembly Name=SystemXml %>
<%@ Import Namespace=SystemXml %>
<%@ Import Namespace=SystemXmlXsl %>
<html>
<head>
<title>
</title>
<script language=c# runat=server
public string xslt()
{
StringWriter writer = new StringWriter();
//裝入xml對象
XmlDocument xmldoc= new XmlDocument();
xmldocLoad(ServerMapPath(Contentsxml));
//裝入xsl對象
XslTransform xsldoc = new XslTransform();
xsldocLoad(ServerMapPath(newsxsl));
//把xml轉化成html頁面
DocumentNavigator nav= new DocumentNavigator(xmldoc);
xsldocTransform(navnullwriter);
return writerToString();
}
</script>
</head>
<body>
<%=xslt()%>
<p align=center

  該程序由<a href=wwwaspcoolcomhttp://wwwaspcoolcom>wwwaspcoolcom</a>設計制作

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

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

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

mainaspx
<%@ Import Namespace=System%>
<%@ Page Language=C# Debug=true codepage=%>
<%@ Import Namespace=SystemIO %>
<%@ Assembly Name=SystemXml %>
<%@ Import Namespace=SystemXml %>
<%@ Import Namespace=SystemXmlXsl %>
<html>
<head>
<title>
</title>
<script language=c# runat=server
public string xslt()
{
StringWriter writer = new StringWriter();
XmlDocument xmldoc= new XmlDocument();
xmldocLoad(ServerMapPath(Request[name] +xml));
XslTransform xsldoc = new XslTransform();
xsldocLoad(ServerMapPath(mainxsl));
DocumentNavigator nav= new DocumentNavigator(xmldoc);
xsldocTransform(navnullwriter);
return writerToString();
}
</script>
</head>
<body>
<%=xslt()%>
<p align=center>該程序由<a href=wwwaspcoolcomhttp://wwwaspcoolcom>wwwaspcoolcom</a>設計制作</p>
</body>
</html> 

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

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

manageaspx
<%@ Import Namespace=SystemXmlXsl %>
<%@ Import Namespace=SystemXml %>
<%@ Assembly Name=SystemXml %>
<%@ Import Namespace=SystemIO %>
<%@ Page Language=C# Debug=true codepage=%>
<%@ Import Namespace=System%>
<HTML>
<HEAD>
<script language=C# runat=server
public void Button_Click(object sender SystemEventArgs e)
{
//判斷文件是否存在
if(FileExists(ServerMapPath(TextBoxText +xml)))
{
ResponseWrite(文件名已經存在請重選文件名);
ResponseEnd() ;
}
else
{
XmlNode currNode;
XmlDocument xmldoc = new XmlDocument();
xmldocLoad(ServerMapPath(contentsxml));
string InsStr=<topic><title>+TextBoxText+</title><href>
mainaspx?name=+TextBoxText+</href></topic>;
XmlDocumentFragment docFrag = xmldocCreateDocumentFragment();
docFragInnerXml = InsStr;
currNode = xmldocDocumentElement;
currNodeInsertAfter(docFrag currNodeLastChild);
//save the output to a file
xmldocSave (ServerMapPath(contentsxml));
//把TextBox中的文件換成符合xml格式的內容
string xmlfile =TextBoxTextReplace(&&);
xmlfile = xmlfileReplace();
xmlfile = xmlfileReplace();
xmlfile = xmlfileReplace( @);
xmlfile = xmlfileReplace(');
xmlfile = xmlfileReplace (\n</paragraph><paragraph>);
//把數據寫入新建的xml文件中去
XmlDocument doc = new XmlDocument();
docLoadXml (<?xml version= encoding=GB?>
<document><title>+TextBoxText +</title><abstract>+
TextBoxText </abstract><author>+TextBoxText+
</author><content><paragraph>+xmlfile+</paragraph>
</content></document>);
docSave (ServerMapPath(TextBoxText +xml));
ResponseWrite(You hava input the article!);
TextBoxText=;
TextBoxText=;
TextBoxText=;
TextBoxText=;
TextBoxText=;
}
//向目錄文件中寫數據
}
public void Button_Click(object sender SystemEventArgs e)
{}
</script>
<meta content=Internet Explorer name=vs_targetSchema>
<meta content=Microsoft Visual Studio name=GENERATOR>
<meta content=C# name=CODE_LANGUAGE>
</HEAD>
<body MS_POSITIONING=GridLayout
<form runat=server
<FONT face=宋體>
<asp:label id=Label style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
asp酷技術資訊網網站內容發布系統
</asp:label>
<asp:label id=Label style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
文件名
</asp:label>
<asp:textbox id=TextBox style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
</asp:textbox>
<asp:label id=Label style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
文章名稱
</asp:label>
<asp:textbox id=TextBox style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
</asp:textbox>
<asp:label id=Label style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
作者
</asp:label>
<asp:textbox id=TextBox style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
</asp:textbox>
<asp:label id=Label style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
摘要
</asp:label>
<asp:textbox id=TextBox style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
</asp:textbox>
<asp:label id=Label style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
內容
</asp:label>
<asp:textbox id=TextBox style=ZINDEX: ; LEFT: px; POSITION:
absolute; TOP: px runat=server Height=px Width=px
textmode=MultiLine
</asp:textbox>
</FONT>


<INPUT id=Button style=ZINDEX: ; LEFT: px; WIDTH: px;
POSITION: absolute; TOP: px; HEIGHT: px type=button value=重置
name=Button runat=server OnServerClick=Button_Click DESIGNTIMEDRAGDROP=
<br>
<br>
<div id=mess runat=server>
</div>
<br>
<input type=button value=提交 OnServerClick=Button_Click
runat=server ID=Button NAME=Button style=ZINDEX: ;
LEFT: px; POSITION: absolute; TOP: px
</form>
</body>
</HTML> 
 
  此程序在net beta build 下測試通過


From:http://tw.wingwit.com/Article/program/net/201311/15780.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.