This post shows "How to create a Rss Reader using Asp Dot Net C#" and bind the result to the Asp Dot Net DataList Control.For this tutorial I am rss feed of faulty plus website.
Html Tag:
<form id="form1" runat="server">
<div>
<h2 class="w3-container w3-teal">
Rss Reader Example</h2>
<hr />
<div class="w3-container">
<div class="w3-row">
<div class="w3-col m1">
<asp:Label ID="lblReader" runat="server" Text="Feed Url:"></asp:Label>
</div>
<div class="w3-col m3">
<asp:TextBox ID="txtFeedUrl" runat="server" CssClass="w3-input w3-border w3-small"></asp:TextBox>
</div>
<div class="w3-col-m3">
<asp:Button ID="btnRead" runat="server" CssClass="w3-btn w3-teal" Text="Read" OnClick="btnRead_Click" />
</div>
</div>
</div>
<br />
<table class="w3-table-all w3-blue w3-text-black">
<thead>
<tr class="w3-teal">
<td>
College Name
</td>
<td>
Website Link
</td>
</tr>
</thead>
<tbody>
<asp:DataList ID="dList" runat="server">
<ItemTemplate>
<tr>
<td>
<strong> <%# Eval("Title")%> </strong>
</td>
<td>
<a href=' <%# Eval("WebsiteLink")%> ' target="_blank">
<%# Eval("WebsiteLink")%></a>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="w3-light-grey">
<td>
<strong> <%# Eval("Title")%> </strong>
</td>
<td>
<a href=' <%# Eval("WebsiteLink")%> ' target="_blank">
<%# Eval("WebsiteLink")%></a>
</td>
</tr>
</AlternatingItemTemplate>
</asp:DataList>
</tbody> </table>
</div>
</form>
Code Behind:
DataTable dt = new DataTable();
private XmlTextReader rssReader;
private XmlDocument rssDoc;
private XmlNode nodeRss;
private XmlNode nodeChannel;
private XmlNode nodeItem;
private void ReadFeedUrl()
{
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("WebsiteLink", typeof(string));
rssReader = new XmlTextReader(txtFeedUrl.Text.ToString());
rssDoc = new XmlDocument();
rssDoc.Load(rssReader);
for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
{
if (rssDoc.ChildNodes[i].Name == "rss")
{
nodeRss = rssDoc.ChildNodes[i];
}
}
for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
{
if (nodeRss.ChildNodes[i].Name == "channel")
{
nodeChannel = nodeRss.ChildNodes[i];
}
}
for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
{
if (nodeChannel.ChildNodes[i].Name == "item")
{
nodeItem = nodeChannel.ChildNodes[i];
if (nodeItem["link"].InnerText != null)
{
string sTitle = nodeItem["title"].InnerText;
string sWebsiteLink = nodeItem["link"].InnerText;
dt.Rows.Add(sTitle, sWebsiteLink);
}
else
{
}
}
}
dList.DataSource = dt;
dList.DataBind();
}
protected void btnRead_Click(object sender, EventArgs e)
{
ReadFeedUrl();
}
Output:
Download Source Code
No comments:
Post a Comment