动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   .Net → 阅读文章

 用ASP.NET和XML做的新闻系统

作者来源: 
阅读 数 380 人次 , 2006-3-29 4:33:00 


  这里我就用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="www.aspcool.comhttp://www.aspcool.com">www.aspcool.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="www.aspcool.comhttp://www.aspcool.com">www.aspcool.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(""","&apos;");
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>

  此程序在.net beta2 build 9148下测试通过。

 本文TagsC#  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:ASP.Net+XML打造留言薄
· 下一篇:创建基于ASP.NET的SMTP邮件服务
· VS.NET: 通过Web服务瞄准电子商务
· JSP教程(六)-怎么在JSP中跳转到别一页面
· 在ASP.NET中处理datetime的一些通用函数(VB)
· 通过事例学习.net的WebForms技术(五)
· 两个获取http页面的c#函数


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077