asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

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


发布日期:2022年10月30日
 
用ASP.NET和XML做的新闻系统

这里我就用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 下测试通过

               

上一篇:ASP.NET入门教程 11.5 启用用户配置文件

下一篇:ASP.NET入门教程 11.6 构建配置文件[2]