如何建立自己的新聞發(fā)布系統(tǒng)?
2024-07-21 02:28:29
供稿:網(wǎng)友
下面是一個(gè)建立新聞發(fā)布系統(tǒng)的程序,不用和數(shù)據(jù)庫打交道哦
步驟:
(1).在vs2005中新建網(wǎng)站,新建三個(gè)aspx網(wǎng)頁,分別命名:title.aspx,news.aspx,main.aspx其中title.aspx用來設(shè)置標(biāo)題,可以自己設(shè)計(jì),寫幾個(gè)字也行,news.aspx用來顯示新聞標(biāo)題,main.aspx用來顯示新聞內(nèi)容。
(2).新建htm頁,用來設(shè)計(jì)框架。代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>無標(biāo)題頁</title>
</head>
<frameset border="0" bordercolor="#6699cc" framespacing="3" id="fstop" rows="85,*">
<frame frameborder="no" marginheight="0" marginwidth="0" name="title" noresize scrolling="no" src="title.aspx" >
<frameset border="0" frameborder="1" framespacing="3" cols="110,*">
<frame frameborder="0" marginheight="0" marginwidth="0" name="dir" src="news.aspx" scrolling="auto" >
<frame bordercolor="#6699cc" frameborder="0" name="main" scrolling="yes" src="main.aspx?name=hello" >
</frameset>
</frameset>
</html>(3).現(xiàn)在你的程序還沒有內(nèi)容,新建xml文件,命名contents.xml,用來顯示新聞標(biāo)題,代碼如下:
<?xml version="1.0" encoding="gb2312"?>
<topiclist type="dreamsite news">
<topic>
<title>歡迎進(jìn)入千里之外的新聞網(wǎng)!</title>
<href>main.aspx?name=hello</href>
</topic>
<topic>
<title>測試新聞</title>
<href>main.aspx?name=test</href>
</topic>
<topic>
<title> 第一條新聞</title>
<href>main.aspx?name=first</href>
</topic>
<topic>
<title> 第二條新聞</title>
<href>main.aspx?name=dddd</href>
</topiclist>注意我們的每條新聞內(nèi)容都是一個(gè)xml文件,現(xiàn)在你知道該做什么了吧,新建hello.xml;test.xml;first.xml;dddd.xml文件,在這里給出一個(gè)例子,代碼如下:
<?xml version="1.0" encoding="gb2312"?>
<document>
<title>今日新聞</title>
<abstract>頭版新聞</abstract>
<author>千里之外</author>
<content><paragraph>大家好</paragraph></content>
</document>現(xiàn)在你就可以看到效果了,下面我們看一下發(fā)布新聞的代碼,也很簡單,新建manage.aspx頁面,前臺(tái)代碼:
</head>
<body>
<form id="form1" runat="server">
<div>
<p>千里之外個(gè)人新聞發(fā)布系統(tǒng)
</p>
文件名:
<asp:textbox id="textbox1" runat="server" />
<asp:requiredfieldvalidator id="valid1" controltovalidate="textbox1" runat="server">(必要欄)
</asp:requiredfieldvalidator><p>
文章名稱:
<asp:textbox id="textbox2" runat="server" />
<asp:requiredfieldvalidator id="valid2" controltovalidate="textbox2" runat="server">(必要欄)
</asp:requiredfieldvalidator>
<p>
作者:
<asp:textbox id="textbox3" runat="server" />
<asp:requiredfieldvalidator id="valid3" controltovalidate="textbox3" runat="server">(必要欄)
</asp:requiredfieldvalidator>
<p>
摘要:<p>
<asp:textbox id="textbox4" textmode="multiline" width="70%" runat="server" />
<p>
內(nèi)容:<p>
<asp:textbox id="textbox5" textmode="multiline" rows="6" width="70%" runat="server" />
<p>
<asp:button id="submit" text="提交" runat="server" />
</div>
</form>
</body>后臺(tái)代碼如下:
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.io;
using system.xml;
public partial class manage : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
//判斷文件是否存在
if (file.exists(server.mappath(textbox1.text + ".xml")))
response.write("文件名已經(jīng)存在,請(qǐng)重新選文件名!");
else
{
//首先將數(shù)據(jù)插入新聞列表中
xmlnode currnode;
xmldocument xmldoc = new xmldocument();
//加載索引文件
xmldoc.load(server.mappath("contents.xml"));
string insstr;
//生成新聞索引
insstr = "<topic><title>" + textbox2.text + "</title><href>main.aspx?name=" + textbox1.text + "</href></topic>";
xmldocumentfragment docfrag = xmldoc.createdocumentfragment();
docfrag.innerxml = insstr;
currnode = xmldoc.documentelement;
//插入新聞索引隊(duì)列中
currnode.insertafter(docfrag, currnode.lastchild);
//保存索引文件
xmldoc.save(server.mappath("contents.xml"));
//把textbox5中的文件換成符合xml格式的內(nèi)容
string xmlfile;
xmlfile = textbox5.text;
xmlfile = xmlfile.replace("<", "+lt; ");
xmlfile = xmlfile.replace(">", "+gt; ");
xmlfile = xmlfile.replace("http://", "+apos;");
xmlfile = xmlfile.replace("/r/n", "</paragraph><paragraph>");
//把數(shù)據(jù)寫入新建的xml文件中去;
xmldocument doc;
doc = new xmldocument();
insstr = "<?xml version='1.0' encoding='gb2312'?><document><title>";
insstr += textbox2.text + "</title><abstract>" + textbox4.text;
insstr += "</abstract><author>" + textbox3.text + "</author><content><paragraph>";
insstr += xmlfile + "</paragraph></content></document>";
doc.loadxml(insstr);
doc.save(server.mappath(textbox1.text + ".xml"));
response.write("新聞發(fā)布成功!");
textbox1.text = "";
textbox2.text = "";
textbox3.text = "";
textbox4.text = "";
textbox5.text = "";
}
}
}
現(xiàn)在,一個(gè)簡單的新聞發(fā)布系統(tǒng)已經(jīng)建立完畢,你還可以編寫更新程序,可以參照發(fā)布程序來寫,有什么好的辦法希望我們共享哦!網(wǎng)站運(yùn)營seo文章大全提供全面的站長運(yùn)營經(jīng)驗(yàn)及seo技術(shù)!