C#读取XML显示到页面的问题
在网上找了个XML存数据的新闻系统,是bate2版的.改了一下,可以运行了.但是显示不出东西来.不知道怎么回事儿.请各位帮忙.
显示新闻的程序news.aspx主要代码如下:
private void Page_Load(object sender, System.EventArgs e)
{
this.Label1.Text = xslt();// 在此处放置用户代码以初始化页面
}
public string xslt()
{
StringWriter writer = new StringWriter();
//装入xml对象
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(Server.MapPath("Contents.xml"));
//装入xsl对象
XslTransform xsldoc = new XslTransform();
xsldoc.Load(Server.MapPath("news.xsl"));
//把xml转化成html页面
//DocumentNavigator nav= new DocumentNavigator(xmldoc);
//xsldoc.Transform(nav,null,writer);
System.Xml.XPath.XPathNavigator nav= xmldoc.CreateNavigator();
xsldoc.Transform(nav,null,writer,null);
return writer.ToString();
}
XML文件的内容:
<?xml version="1.0" encoding="GB2312"?>
<topiclist xmlns="http://tempuri.org/contents.xsd" type="AspCool News">
<topic>
<title>aspcool news!</title>
<href>main.aspx?name=hello</href>
</topic>
<topic>
<title>Resolve a problem</title>
<href>main.aspx?name=test</href>
</topic>
<topic xmlns="">
<title>测试</title>
<href>main.aspx?name=2005</href>
</topic>
<topic xmlns="">
<title>fasdf</title>
<href>main.aspx?name=asdfas</href>
</topic>
</topiclist>
XSL文件news.xsl的内容:
<?xml version="1.0" encoding="GB2312" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="topiclist/topic">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="href"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>