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

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"); 
  }
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值