我正在尝试将标题和日期提取到最后三个wordpress博客帖子的主页。原谅我,因为我根本不懂.net。
在我的default.aspx页面顶部,我有这个命令:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DefaultNew.aspx.cs" Inherits="_DefaultNew" Debug="false" %>
<%@ Register Src="DefaultWordpress.aspx.cs" TagPrefix="Site" TagName="Wordpress" %>
<%@ Register Src="~/includes/siteHomeHeaderNew.ascx" TagPrefix="Site" TagName="Header" %>
<%@ Register Src="~/includes/siteFooter.ascx" TagPrefix="Site" TagName="Footer" %>
我想要标题的内容我有:
<Site:Wordpress runat="server" />
然后在我的DefaultWordpress.aspx.cs文件中,我有这个:
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Data;
using System.Configuration;
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.Collections;
using System.Xml;
public partial class Wordpress : System.Web.UI.Page
{
using (var reader = XmlReader.Create("http://MY_SITE_GOES_HERE.com/blog/index.php?feed=atom"))
{
var feed = SyndicationFeed.Load(reader);
// Print title and summary of 3 most recent posts
foreach (var post in feed.Items.Take(3))
{
Console.WriteLine ("--- {0} ---", post.Title.Text);
Console.WriteLine (post.Summary.Text);
Console.WriteLine();
}
}
}
编译错误说明了这一点:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1519: Invalid token 'using' in class, struct, or interface member declaration
来源错误:
Line 16: public partial class Wordpress : System.Web.UI.Page
Line 17: {
Line 18: using (var reader = XmlReader.Create("http://www.MY_SITE_HERE.com/blog/index.php?feed=atom"))
Line 19: {
Line 20: var feed = SyndicationFeed.Load(reader);
Source File: \\fs3-n02\stor9wc1dfw1\646363\647414\www.MY_SITE_HERE.com\web\content\DefaultWordpress.aspx.cs Line: 18
这显然不起作用,但我不知道从哪里开始。任何见解将不胜感激。有没有更简单的方法来做到这一点,或者只是我缺少的东西?
谢谢!!答案 0 :(得分:0)
您需要将using
语句放在方法中,例如Page_Load
或您想要的任何其他方法。
除此之外,您无法在网页上使用Console
。
将代码放在方法中可能如下所示:
protected void Page_Load(object sender, EventArgs e)
{
using (var reader = XmlReader.Create("http://MY_SITE_GOES_HERE.com/blog/index.php?feed=atom"))
{
var feed = SyndicationFeed.Load(reader);
// Print title and summary of 3 most recent posts
foreach (var post in feed.Items.Take(3))
{
// ... Logic to render post information ...
}
}
}
<强>更新强>
您使用的是没有SyndicationFeed
类的.NET 2.0。您可以尝试使用Atom.NET代替.NET 2.0。