国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:12:04
字體:
供稿:網(wǎng)友
國內(nèi)最大的酷站演示中心!

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

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>

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

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

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轉(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="http://m.survivalescaperooms.com">m.survivalescaperooms.com</a>設(shè)計制作.

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

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

  下面是新聞內(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="http://m.survivalescaperooms.com">m.survivalescaperooms.com</a>設(shè)計制作</p> 
</body>
</html>

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

  最后,大家來看一下最負責的一個頁面,這個頁面的作用就是用來建立新的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)存在,請重選文件名。");
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(""","'");
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 runat="server" height="28px" width="156px">
asp酷技術(shù)資訊網(wǎng)網(wǎng)站內(nèi)容發(fā)布系統(tǒng)
</asp:label>
<asp:label id=label2 runat="server" height="25px" width="65px">
文件名:
</asp:label>
<asp:textbox id=textbox1 runat="server" height="33px" width="178px" >
</asp:textbox>
<asp:label id=label3 runat="server" height="36px" width="86px">
文章名稱:
</asp:label>
<asp:textbox id=textbox2 runat="server" height="37px" width="177px">
</asp:textbox>
<asp:label id=label4 runat="server" height="31px" width="89px">
作者:
</asp:label>
<asp:textbox id=textbox3 runat="server" height="36px" width="179px">
</asp:textbox>
<asp:label id=label5 runat="server" height="51px" width="81px">
摘要:
</asp:label>
<asp:textbox id=textbox4 runat="server" height="36px" width="179px">
</asp:textbox>
<asp:label id=label6 runat="server" height="36px" width="78px">
內(nèi)容:
</asp:label>
<asp:textbox id=textbox5 runat="server" height="95px" width="252px"
textmode="multiline">
</asp:textbox>
</font>

<input id=button2  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" >
</form>
</body>
</html>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江安县| 岑巩县| 绥芬河市| 贵州省| 桓台县| 淮北市| 白山市| 彰化市| 基隆市| 宝鸡市| 镇平县| 潮安县| 株洲市| 冷水江市| 乐至县| 奉节县| 建德市| 兴隆县| 尉犁县| 天长市| 蒙自县| 新绛县| 沙洋县| 安新县| 安远县| 江安县| 郯城县| 南充市| 福泉市| 双牌县| 永兴县| 芮城县| 荔浦县| 邵阳市| 明水县| 梅州市| 加查县| 咸阳市| 渑池县| 马尔康县| 咸阳市|