0

我正在尝试将最后三个 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 

这显然行不通,但我不确定从这里去哪里。任何见解将不胜感激。有没有更简单的方法可以做到这一点,或者只是我缺少的东西?

谢谢你!!

4

1 回答 1

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。您可以尝试使用应该支持 .NET 2.0的Atom.NET 。

于 2012-05-10T21:07:25.213 回答