利用xsl文件对xml文件倒序排序并显示

这个是把对新闻的评论保存到一个指定的xml文件中,只是一个对xml文件的读写过程。环境是vs2005。

1xml文件Comment.xml

<?xml version="1.0" encoding="utf-8"?>
<AllComment>
   
<Comment NewsId="152">
     
<CommentContent>3333333333</CommentContent>
     
<AddTime>2007-5-28 22:41:08</AddTime>
   
</Comment>
   
<Comment NewsId="153">
     
<CommentContent>4444</CommentContent>
     
<AddTime>2007-5-28 22:41:48</AddTime>
   
</Comment>
   
<Comment NewsId="154">
     
<CommentContent>hhhhhhhhhh</CommentContent>
     
<AddTime>2007-5-28 22:41:52</AddTime>
   
</Comment>
   
<Comment NewsId="151">
     
<CommentContent>fff</CommentContent>
     
<AddTime>2007-5-28 22:42:03</AddTime>
   
</Comment>
   
<Comment NewsId="154">
     
<CommentContent>bbbbb</CommentContent>
     
<AddTime>2007-5-28 23:04:23</AddTime>
   
</Comment>
</AllComment>

2、.aspx.cs文件,对xml文件进行读写

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.Xml;

public partial class NewsFiles_AddComment : System.Web.UI.Page
{
    
//protected System.Web.UI.WebControls XMLNodeListBox;
    protected void Page_Load(object sender, EventArgs e)
    
{
        
string strNewsId = Request.Form["NewsId"].Trim().ToString();
        
string strCommentContent = Request.Form["CommentContent"].Trim().ToString();
        System.Xml.XmlDocument doc 
= new System.Xml.XmlDocument();//创建对象
        doc.Load(Server.MapPath(
"../XmlFiles/Comment.xml"));//倒入文件
        XmlElement childElem;
        childElem 
= doc.CreateElement("Comment");//创建子节点
        childElem.SetAttribute(
"NewsId""", strNewsId);
        XmlElement commentChild;
        commentChild 
= doc.CreateElement("CommentContent");
        commentChild.InnerText 
= strCommentContent;
        childElem.AppendChild(commentChild);
        commentChild 
= doc.CreateElement("AddTime");
        commentChild.InnerText 
= DateTime.Now.ToString();
        childElem.AppendChild(commentChild);
        XmlNode node;
        node 
= doc.SelectSingleNode("//AllComment");
            node.AppendChild(childElem);//添加子节点
            doc.Save(Server.MapPath(
"../XmlFiles/Comment.xml"));//保存.xml文件
            Response.Redirect(
"DisplayComment.aspx?NewsId=" + strNewsId + "");
    }

}

前台页面就是一个评论内容。不要直接拷贝使用此代码,代码中的路径需要改一下。我的blog中的程序都是经过测试的。但基本上都需要稍加调整

posted @ 2008-06-25 23:39  专注是什么  阅读(696)  评论(0编辑  收藏  举报