如何循环使用类型SyndicationFeed输出RSS以查看?

时间:2012-08-14 13:54:41

标签: asp.net-mvc vb.net asp.net-mvc-3

我想在我的视图中输出我的RSS提要,如下所示:

@ModelType IEnumerable(Of MyBlog.RssModel)

<table>
    <tr>
        <th>
            Title
        </th>
        <th>
            Description
        </th>
        <th>
            Link
        </th>
        <th></th>
    </tr>

@For Each item In Model
    Dim currentItem = item
    @<tr>
        <td>
            @Html.DisplayFor(Function(modelItem) currentItem.Title)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) currentItem.Description)
        </td>
        <td>
            @Html.DisplayFor(Function(modelItem) currentItem.Link)
        </td>
        <td>
        </td>
    </tr>
Next

</table>

这是我的代码:

Function ShowFeed() As ActionResult

    Dim feedUrl = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml"
    Dim feed As SyndicationFeed = GetFeed(feedUrl)

    Dim model As IList(Of RssModel) = New List(Of RssModel)()

    For Each item As SyndicationItem In feed.Items
        Dim rss As New RssModel()
        rss.Title = item.Title.ToString
        rss.Description = item.Summary.ToString
        rss.Link = item.Links.ToString

        model.Add(rss)
    Next

    Return View(model)

End Function

产生意外结果:

  

标题说明链接
  System.ServiceModel.Syndication.TextSyndicationContent
  System.ServiceModel.Syndication.TextSyndicationContent
  System.ServiceModel.Syndication.NullNotAllowedCollection 1[System.ServiceModel.Syndication.SyndicationLink]
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.TextSyndicationContent
System.ServiceModel.Syndication.NullNotAllowedCollection
1 [System.ServiceModel.Syndication.SyndicationLink]
  System.ServiceModel.Syndication.TextSyndicationContent
  System.ServiceModel.Syndication.TextSyndicationContent
  System.ServiceModel.Syndication.NullNotAllowedCollection`1 [System.ServiceModel.Syndication.SyndicationLink]

2 个答案:

答案 0 :(得分:1)

您的Return View(viewModel)正在返回单个RssModel,而不是RssModel列表。您应该创建一个IEnumerable(RssModel)并在For Each循环中填充它,然后将IEnumerable返回到View。

  
    

编辑:使用从c#到vb的代码转换器,但这应该会显示进度。

  
Dim model As IList(Of RssModel) = New List(Of RssModel)()

For Each item As var In feed
    Dim rss As New RssModel()
    rss.Something = item.Something

    model.Add(rss)
Next

Return View(model.AsEnumerable(Of RssModel)())

答案 1 :(得分:0)

答案是:

Function ShowFeed() As ActionResult

        Dim feedUrl = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml"
        Dim feed As SyndicationFeed = GetFeed(feedUrl)

        Dim model As IList(Of RssModel) = New List(Of RssModel)()

        For Each item As SyndicationItem In feed.Items
            Dim rss As New RssModel()
            rss.Title = item.Title.Text
            rss.Description = item.Summary.Text
            rss.Link = item.Links.First.Uri.ToString

            model.Add(rss)
        Next

        Return View(model)

    End Function