在ASP.NET中显示XML内容(以常见的公告栏为例)

之前写过一片类似的文章,被R2仁兄指点了一番,发现里面有问题,所以就重新整理了一个,并调试通过,这个网上类似的也很多,只是自己想整理一下,以后就不用再找了。
1. 公告栏(新闻列表)
newslist.xml 

<?xml version="1.0" encoding="GB2312"?>
<topiclist type="AspCool News">
<topic>
<title>第一条新闻</title>
<href>main.aspx?name=news1</href>
</topic>
<topic>
<title>第二条新闻</title>
<href>main.aspx?name=new2</href>
</topic>
</topiclist>

shownewslist.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("newslist.xml"));
//装入xsl对象
XslTransform xsldoc = new XslTransform();
xsldoc.Load(Server.MapPath("newslist.xsl"));
//把xml转化成html页面
DocumentNavigator nav= new DocumentNavigator(xmldoc);
xsldoc.Transform(nav,null,writer);
return writer.ToString();
}
</script>
</head>
<body>
<%=xslt()%>
</body></html>

2. 每条新闻的显示同理

3.怎么样新建一条新闻
  news1.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>    

新建news1.xml的方法

private void btnGetXml_Click(object sender, System.EventArgs e)
  {
    stirng filename=this.txtfilename.Test;
    string title=this.txttitle.Text;
    string abstract=this.txtabstract.Text;
    string author=this.txtauthor.Text;
    string content=this.txtcontent.Text;
   //判断文件是否存在
  if(File.Exists(Server.MapPath(filename+".xml")))
  {
  Response.Write("文件名已经存在,请重选文件名。");
  Response.End() ;
  }
  else
  { 
      //create news1.xml
   System.Xml.XmlTextWriter myWriter=new XmlTextWriter(filename+".xml",null);
  myWriter.WriteProcessingInstruction("xml","version='1.0'");
  myWriter.WriteStartElement("content");
  myWriter.WriteAttributeString("type","whitePaper");
//
   myWriter.WriteStartElement("document");
    myWriter.WriteElementString("title",title);
   myWriter.WriteElementString("abstract",abstract);
   myWriter.WriteElementString("author",author);
   myWriter.WriteElementString("content",content);
  //

   myWriter.WriteEndElement();
  myWriter.Flush();
   myWriter.Close();
  }
}

4.怎么样修改现有的newslist.xml
每增加一条新闻,就要在newslist.xml里加上一条

private void Updatenewslist(string title,string href)
  {
   //add new nodes to newslist.xml
   XmlDocument myDocument=new XmlDocument();     
   myDocument.Load("newslist.xml");
   XmlElement topiclist=(XmlElement)myDocument.GetElementsByTagName("topiclist");
   XmlElement topic=myDocument.CreateElement("topic"); 
   XmlElement title= topic.CreateElement("title");
   title.InnerText=title;
   topic.AppendChild(title);
   xmlElement href=topic.CreateElement("href");
   href.InnerText=href;
   topic.AppendChild(href);
   topiclist.PrependChild(topic);
   myDocument.Save("newslist.xml"); 
  }
posted @ 2004-07-02 19:40  bluehat  阅读(4413)  评论(11编辑  收藏  举报