确保您安装了正确的 VS 工作负载和各个组件。
- 打开 Visual Studio 安装程序
- 点击修改
- 点击工作负载标签
- 确保选中 ASP.NET 和 Web 开发,如果没有,请选中。
- 点击单个组件
- 检查所需的 .NET Framework SDK 和目标包(即:
.NET Framework 4.7.2 SDK、.NET Framework 4.7.2 targeting pack、.NET Framework 4.8 SDK 和 .NET Framework 4.8 targeting pack)
- 如果您进行了任何更改,请在右下角选择全部下载,然后安装。然后点击修改。
然后尝试以下操作:
注意:下面的代码是从here转换成VB.NET的。但是,班级名称已更改。为了进行测试,我使用了 .NET Framework 4.8 版,但其他版本也可以使用。
VS 2019:
创建一个新项目
打开解决方案资源管理器
添加类(名称:RSSFeed.vb)
- 在解决方案资源管理器中,右键单击 (例如:RSSFeedReader)
- 选择添加
- 选择类...(名称:RSSFeed.vb)
- 点击添加
RSSFeed.vb
Public Class RSSFeed
Public Property Title As String
Public Property Link As String
Public Property PublishDate As String
Public Property Description As String
End Class
添加WebForm(名称:default.aspx)
- 在解决方案资源管理器中,右键单击 (例如:RSSFeedReader)
- 选择添加
- 选择新项目...
- 选择Web 表单(名称:default.aspx)
- 点击添加
修改 default.aspx
- 在解决方案资源管理器中,右键单击 default.aspx
- 选择查看标记
default.aspx:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="RSSFeedReader._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<h3>Read RSS Feeds</h3>
<form id="Form1" runat="server" >
<!-- Where XYZ refers to the publication from where you wish to fetch the RSS feed from -->
<div style="max-height:350px; overflow:auto">
<asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
</td>
<td width="200px">
<%#Eval("PublishDate") %>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
<%#Eval("Description") %>
</td>
</tr>
<tr>
<td> </td>
<td align="right">
<a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
修改 default.aspx.vb
- 在解决方案资源管理器中,右键单击 default.aspx
- 选择查看代码
default.aspx.vb:
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ToDo: replace with the URL to your desired RSS feed
Dim rssFeedUrls As List(Of String) = New List(Of String)()
'add desired URLs
rssFeedUrls.Add("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
rssFeedUrls.Add("http://thehill.com/rss/syndicator/19110")
'get data
PopulateRssFeed(rssFeedUrls)
End Sub
Private Sub PopulateRssFeed(rssFeedUrls As List(Of String))
'create new List
Dim feeds As List(Of RSSFeed) = New List(Of RSSFeed)
Dim currentURL As String = String.Empty
Try
For Each url As String In rssFeedUrls
'set value
currentURL = url
'create new instance
Dim xDoc As XDocument = New XDocument()
'load
xDoc = XDocument.Load(url)
Dim items = From x In xDoc.Descendants("item")
Select New RSSFeed With
{
.Title = x.Element("title").Value,
.Link = x.Element("link").Value,
.PublishDate = x.Element("pubDate").Value,
.Description = x.Element("description").Value
}
If items IsNot Nothing Then
For Each i In items
Dim f As RSSFeed = New RSSFeed() With {
.Title = i.Title,
.Link = i.Link,
.PublishDate = i.PublishDate,
.Description = i.Description
}
'add
feeds.Add(f)
Next
End If
Next
gvRss.DataSource = feeds
gvRss.DataBind()
Catch ex As Exception
'ToDo: replace with desired code
Dim errMsg As String = String.Format("Error (PopulateRssFeed): {0} (url: {1})", ex.Message, currentURL)
Debug.WriteLine(errMsg)
Throw ex
End Try
End Sub
End Class
确保已安装/打开以下 IIS 功能:(Win 7)
更新:
_default在“default.aspx.designer.vb”中定义
打开 default.aspx.designer.vb:
- 在解决方案资源管理器中,右键单击 default.aspx
- 选择查看代码生成文件
default.aspx.designer.vb
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Option Strict On
Option Explicit On
Partial Public Class _default
'''<summary>
'''Form1 control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents Form1 As Global.System.Web.UI.HtmlControls.HtmlForm
'''<summary>
'''gvRss control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents gvRss As Global.System.Web.UI.WebControls.GridView
End Class
更新 2:
创建项目时,如果选择“创建新的 ASP.NET Web 应用程序”:
然后在“Default.aspx”中使用下面的代码代替上面的代码:
Default.aspx:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="RSSFeedReader._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h3>Read RSS Feeds</h3>
<!-- Where XYZ refers to the publication from where you wish to fetch the RSS feed from -->
<div style="max-height:350px; overflow:auto">
<asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
</td>
<td width="200px">
<%#Eval("PublishDate") %>
</td>
</tr>
<tr>
<td colspan="2">
<hr />
<%#Eval("Description") %>
</td>
</tr>
<tr>
<td> </td>
<td align="right">
<a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</asp:Content>
注意:“Default.aspx.vb”的代码是一样的。
更新 3:
根据这个video,如果你想查看一个YT RSS提要,你需要使用下面的URL:https://www.youtube.com/feeds/videos.xml?channel_id=<channel_id>
注意: 是 URL 的最后一部分(即:在最后一个 / 之后)。根据视频,有15个条目的限制。
此外,还需要为 XML 使用命名空间。因此,对“default.aspx.vb”使用以下内容:
default.aspx.vb:
Public Class _default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'ToDo: replace with the URL to your desired RSS feed
Dim rssFeedUrls As List(Of String) = New List(Of String)()
'add desired URLs
rssFeedUrls.Add("https://www.youtube.com/feeds/videos.xml?channel_id=UCBB7sYb14uBtk8UqSQYc9-w")
'get data
PopulateRssFeedYT(rssFeedUrls)
End Sub
Private Sub PopulateRssFeedYT(rssFeedUrls As List(Of String))
'create new List
Dim feeds As List(Of RSSFeed) = New List(Of RSSFeed)
Dim currentURL As String = String.Empty
Try
For Each url As String In rssFeedUrls
'set value
currentURL = url
'create new instance
Dim xDoc As XDocument = New XDocument()
'load
xDoc = XDocument.Load(url)
Dim items = From x In xDoc.Descendants("{http://www.w3.org/2005/Atom}entry")
Select New RSSFeed With
{
.Title = x.Element("{http://www.w3.org/2005/Atom}title").Value,
.Link = x.Element("{http://www.w3.org/2005/Atom}link").Attribute("href").Value,
.PublishDate = x.Element("{http://www.w3.org/2005/Atom}published").Value,
.Description = x.Element("{http://search.yahoo.com/mrss/}group").Element("{http://search.yahoo.com/mrss/}description").Value
}
If items IsNot Nothing Then
For Each i In items
Dim f As RSSFeed = New RSSFeed() With {
.Title = i.Title,
.Link = i.Link,
.PublishDate = i.PublishDate
}
If i.Description.Length <= 50 Then
f.Description = i.Description
Else
'only show the first 50 chars
f.Description = i.Description.Substring(0, 50)
End If
'add
feeds.Add(f)
Next
End If
Next
gvRss.DataSource = feeds
gvRss.DataBind()
Catch ex As Exception
'ToDo: replace with desired code
Dim errMsg As String = String.Format("Error (PopulateRssFeed): {0} (url: {1})", ex.Message, currentURL)
Debug.WriteLine(errMsg)
Throw ex
End Try
End Sub
End Class
注意:对于检索所需数据的替代方法,以下可能会有所帮助:
资源: