这里我就用xml代替数据,写一个新闻发布系统,希望能够起到抛砖引玉的作用,使更多的人能够了解这些最新的技术。下面介绍这几个文件。
contents.xml
  1. <?xml version="1.0" encoding="GB2312"?>
  2. <topiclist type="AspCool News">
  3. <topic>
  4. <title>aspcool news!</title>
  5. <href>main.aspx?name=hello</href>
  6. </topic>
  7. <topic>
  8. <title>Resolve a problem</title>
  9. <href>main.aspx?name=test</href>
  10. </topic>
  11. </topiclist>
复制代码
这是一个很简单的xml文件,它的作用是用来显示新闻的列表。
hello.xml
  1. <?xml version="1.0" encoding="GB2312"?>
  2. <document>
  3. <title>aspcool news!</title>
  4. <abstract>test news</abstract>
  5. <author>feiying</author>
  6. <content>
  7. <paragraph>The firet test</paragraph>
  8. </content>
  9. </document>
复制代码
这个文件是用来显示新闻的内容,其中各个意思大家一看就明白,我就不在这儿多说了。
下面给大家看新闻列表显示的页面。
news.aspx
  1. <%@ Import Namespace="System"%>
  2. <%@ Page Language="C#" Debug="true" codepage="936"%>
  3. <%@ Import Namespace="System.IO" %>
  4. <%@ Assembly Name="System.Xml" %>
  5. <%@ Import Namespace="System.Xml" %>
  6. <%@ Import Namespace="System.Xml.Xsl" %>
  7. <html>
  8. <head>
  9. <title>
  10. </title>
  11. <script language="c#" runat="server">
  12. public string xslt()
  13. {
  14. StringWriter writer = new StringWriter();
  15. //装入xml对象
  16. XmlDocument xmldoc= new XmlDocument();
  17. xmldoc.Load(Server.MapPath("Contents.xml"));
  18. //装入xsl对象
  19. XslTransform xsldoc = new XslTransform();
  20. xsldoc.Load(Server.MapPath("news.xsl"));
  21. //把xml转化成html页面
  22. DocumentNavigator nav= new DocumentNavigator(xmldoc);
  23. xsldoc.Transform(nav,null,writer);
  24. return writer.ToString();
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <%=xslt()%>
  30. <p align="center">
  31. 该程序由<a href="www.pin5i.com">拼吾爱程序人生</a>设计制作.
  32. </p>
  33. </body>
  34. </html>
复制代码
这个页面完成了从xml通过xslt转化成html文件,也使我对于xslt有了进一步的认识。
下面是新闻内容显示的页面:
main.aspx
  1. <%@ Import Namespace="System"%>
  2. <%@ Page Language="C#" Debug="true" codepage="936"%>
  3. <%@ Import Namespace="System.IO" %>
  4. <%@ Assembly Name="System.Xml" %>
  5. <%@ Import Namespace="System.Xml" %>
  6. <%@ Import Namespace="System.Xml.Xsl" %>
  7. <html>
  8. <head>
  9. <title>
  10. </title>
  11. <script language="c#" runat="server">
  12. public string xslt()
  13. {
  14. StringWriter writer = new StringWriter();
  15. XmlDocument xmldoc= new XmlDocument();
  16. xmldoc.Load(Server.MapPath(Request["name"]  ".xml"));
  17. XslTransform xsldoc = new XslTransform();
  18. xsldoc.Load(Server.MapPath("main.xsl"));
  19. DocumentNavigator nav= new DocumentNavigator(xmldoc);
  20. xsldoc.Transform(nav,null,writer);
  21. return writer.ToString();
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. <%=xslt()%>
  27. <p align="center">该程序由<a href="www.pin5i.com">拼吾爱程序人生</a>设计制作.</p>
  28. </body>
  29. </html>
复制代码
这个功能和上面的一样,我在这儿就不多说了。 

最后,大家来看一下最负责的一个页面,这个页面的作用就是用来建立新的xml数据。
manage.aspx
  1. <%@ Import Namespace="System.Xml.Xsl" %>
  2. <%@ Import Namespace="System.Xml" %>
  3. <%@ Assembly Name="System.Xml" %>
  4. <%@ Import Namespace="System.IO" %>
  5. <%@ Page Language="C#" Debug="true" codepage="936"%>
  6. <%@ Import Namespace="System"%>
  7. <HTML>
  8. <HEAD>
  9. <script language="C#" runat="server">
  10. public void Button1_Click(object sender, System.EventArgs e)
  11. {
  12. //判断文件是否存在
  13. if(File.Exists(Server.MapPath(TextBox1.Text  ".xml")))
  14. {
  15.   Response.Write("文件名已经存在,请重选文件名。");
  16.   Response.End() ; 
  17. }
  18. else
  19. {
  20.   XmlNode currNode;
  21.   XmlDocument xmldoc = new XmlDocument();
  22.   xmldoc.Load(Server.MapPath("contents.xml"));
  23.   string InsStr="<topic><title>" TextBox2.Text "</title><href>
  24.   main.aspx?name=" TextBox1.Text "</href></topic>";
  25.   XmlDocumentFragment docFrag = xmldoc.CreateDocumentFragment();
  26.   docFrag.InnerXml = InsStr;
  27.   currNode = xmldoc.DocumentElement;
  28.   currNode.InsertAfter(docFrag, currNode.LastChild);
  29.   //save the output to a file
  30.   xmldoc.Save (Server.MapPath("contents.xml"));
  31.   //把TextBox5中的文件换成符合xml格式的内容。
  32.   string xmlfile =TextBox5.Text.Replace("&;amp;","&;amp;");
  33.   xmlfile = xmlfile.Replace("<","<");
  34.   xmlfile = xmlfile.Replace(">",">");
  35.   xmlfile = xmlfile.Replace( @"""""",""");
  36.   xmlfile = xmlfile.Replace(""","&;amp;apos;");
  37.   xmlfile = xmlfile.Replace ("\n","</paragraph><paragraph>");
  38.   //把数据写入新建的xml文件中去。
  39.   XmlDocument doc = new XmlDocument();
  40.   doc.LoadXml ("<?xml version="1.0" encoding="GB2312"?>
  41.   <document><title>" TextBox2.Text  "</title><abstract>"
  42.   TextBox4.Text "</abstract><author>" TextBox3.Text
  43.   "</author><content><paragraph>" xmlfile "</paragraph>
  44.   </content></document>");
  45.   doc.Save (Server.MapPath(TextBox1.Text  ".xml"));
  46.   Response.Write("You hava input the article!");
  47.   TextBox1.Text="";
  48.   TextBox2.Text="";
  49.   TextBox3.Text="";
  50.   TextBox4.Text="";
  51.   TextBox5.Text="";
  52. }
  53. //向目录文件中写数据
  54. }
  55. public void Button2_Click(object sender, System.EventArgs e)
  56. {}
  57. </script>
  58. <meta content="Internet Explorer 5.0" name=vs_targetSchema>
  59. <meta content="Microsoft Visual Studio 7.0" name=GENERATOR>
  60. <meta content=C# name=CODE_LANGUAGE>
  61. </HEAD>
  62. <body MS_POSITIONING="GridLayout">
  63. <form runat="server">
  64. <FONT face=宋体>
  65. <asp:label id=Label1 style="Z-INDEX: 100; LEFT: 230px; POSITION:
  66. absolute; TOP: 27px" runat="server" Height="28px" Width="156px">
  67. asp酷技术资讯网网站内容发布系统
  68. </asp:label>
  69. <asp:label id=Label2 style="Z-INDEX: 101; LEFT: 110px; POSITION:
  70. absolute; TOP: 68px" runat="server" Height="25px" Width="65px">
  71. 文件名:
  72. </asp:label>
  73. <asp:textbox id=TextBox1 style="Z-INDEX: 102; LEFT: 255px; POSITION:
  74. absolute; TOP: 64px" runat="server" Height="33px" Width="178px" >
  75. </asp:textbox>
  76. <asp:label id=Label3 style="Z-INDEX: 103; LEFT: 108px; POSITION:
  77. absolute; TOP: 126px" runat="server" Height="36px" Width="86px">
  78. 文章名称:
  79. </asp:label>
  80. <asp:textbox id=TextBox2 style="Z-INDEX: 104; LEFT: 256px; POSITION:
  81. absolute; TOP: 114px" runat="server" Height="37px" Width="177px">
  82. </asp:textbox>
  83. <asp:label id=Label4 style="Z-INDEX: 105; LEFT: 114px; POSITION:
  84. absolute; TOP: 183px" runat="server" Height="31px" Width="89px">
  85. 作者:
  86. </asp:label>
  87. <asp:textbox id=TextBox3 style="Z-INDEX: 106; LEFT: 256px; POSITION:
  88. absolute; TOP: 183px" runat="server" Height="36px" Width="179px">
  89. </asp:textbox>
  90. <asp:label id=Label5 style="Z-INDEX: 107; LEFT: 114px; POSITION:
  91. absolute; TOP: 241px" runat="server" Height="51px" Width="81px">
  92. 摘要:
  93. </asp:label>
  94. <asp:textbox id=TextBox4 style="Z-INDEX: 108; LEFT: 256px; POSITION:
  95. absolute; TOP: 245px" runat="server" Height="36px" Width="179px">
  96. </asp:textbox>
  97. <asp:label id=Label6 style="Z-INDEX: 109; LEFT: 116px; POSITION:
  98. absolute; TOP: 315px" runat="server" Height="36px" Width="78px">
  99. 内容:
  100. </asp:label>
  101. <asp:textbox id=TextBox5 style="Z-INDEX: 110; LEFT: 259px; POSITION:
  102. absolute; TOP: 303px" runat="server" Height="95px" Width="252px"
  103. textmode="MultiLine">
  104. </asp:textbox>
  105. </FONT> 

  106. <input id=Button2 style="Z-INDEX: 113; LEFT: 343px; WIDTH: 40px;
  107. POSITION: absolute; TOP: 430px; HEIGHT: 24px" type=button value=重置
  108. name=Button2 runat="server" OnServerClick="Button2_Click" DESIGNTIMEDRAGDROP="59">
  109. <br>
  110. <br>
  111. <div id=mess runat=server>
  112. </div>
  113. <br>
  114. <input type="button" value="提交" OnServerClick="Button1_Click"
  115. runat="server" ID="Button1" NAME="Button1" style="Z-INDEX: 112;
  116. LEFT: 268px; POSITION: absolute; TOP: 430px">
  117. </form>
  118. </body>
  119. </HTML>
复制代码
此程序在.net beta2 build 9148下测试通过。
TOP