PDA

View Full Version : سوال: نحوه خواندن rss از سایت تابناک و نمایش در سایت؟



پیام حیاتی
دوشنبه 13 مهر 1394, 14:31 عصر
سلام
فرض کنید می خواهم اخبار اقتصادی در این لینک را :
http://tabnak.ir/fa/rss/6
در سایت به نمایش در بیارم چه کار باید انجام بدم؟
لطفا" یک مثال ساده بزنید.
تشکر

mohammadrezak
چهارشنبه 15 مهر 1394, 09:25 صبح
سلام
من با repeater انجام می دم این کار رو

نمونه :

<asp:XmlDataSource ID="RssFeed" runat="server" DataFile="http://tabnak.ir/fa/rss/6/" XPath="/rss/channel/item"></asp:XmlDataSource>
<asp:Repeater ID="repFeed" runat="server" DataSourceID="RssFeed">
<ItemTemplate>
<div class="col-md-12">
<h5 class="col-md-12"> <a title="<%# XPath("title") %>" href='<%# XPath("link") %>'><%# XPath("title") %>
</a></h5>



</div>
</ItemTemplate>
</asp:Repeater>

پیام حیاتی
چهارشنبه 15 مهر 1394, 13:47 عصر
این الآن جواب نمیده:

<!-- Start -->

<asp:XmlDataSource ID="RssFeed" runat="server" DataFile="http://tabnak.ir/fa/rss/6/" XPath="/rss/channel/item"></asp:XmlDataSource>
<asp:Repeater ID="repFeed" runat="server" DataSourceID="RssFeed">
<ItemTemplate>
<a title="<%# XPath("title") %>" href='<%# XPath("link") %>'><%# XPath("title") %></a>
</ItemTemplate>
</asp:Repeater>


<!-- End -->

mr_ayma
چهارشنبه 15 مهر 1394, 14:01 عصر
سلام ، می تونید از این کد استفاده کنید



post = null;
HttpWebRequest MyRssRequest = (HttpWebRequest)WebRequest.Create(url);
MyRssRequest.AllowWriteStreamBuffering = true;


MyRssRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
MyRssRequest.Referer = "http://www.google.com/";
MyRssRequest.Timeout = 20000;
WebResponse MyRssResponse = MyRssRequest.GetResponse();
Stream MyRssStream = MyRssResponse.GetResponseStream();

XmlDocument MyRssDocument = new XmlDocument();
MyRssDocument.Load(MyRssStream);
XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");
string sTitle = null;
string sLink = null;
string sDescription = null;
// MyRssList.Count
for (int i = 0; i < MyRssList.Count; i++)
{
post = null;
XmlNode MyRssDetail;

MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
if (MyRssDetail != null)
sTitle = MyRssDetail.InnerText;
else
sTitle = null;


MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
if (MyRssDetail != null)
{
sDescription = MyRssDetail.InnerText;

}
else
{
sDescription = null;
}

MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
if (MyRssDetail != null)
{
sLink = MyRssDetail.InnerText;

} post = null;
HttpWebRequest MyRssRequest = (HttpWebRequest)WebRequest.Create(url);
MyRssRequest.AllowWriteStreamBuffering = true;


MyRssRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
MyRssRequest.Referer = "http://www.google.com/";
MyRssRequest.Timeout = 20000;
WebResponse MyRssResponse = MyRssRequest.GetResponse();
Stream MyRssStream = MyRssResponse.GetResponseStream();

XmlDocument MyRssDocument = new XmlDocument();
MyRssDocument.Load(MyRssStream);
XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");
string sTitle = null;
string sLink = null;
string sDescription = null;
// MyRssList.Count
for (int i = 0; i < MyRssList.Count; i++)
{
post = null;
XmlNode MyRssDetail;

MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
if (MyRssDetail != null)
sTitle = MyRssDetail.InnerText;
else
sTitle = null;


MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
if (MyRssDetail != null)
{
sDescription = MyRssDetail.InnerText;

}
else
{
sDescription = null;
}

MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
if (MyRssDetail != null)
{
sLink = MyRssDetail.InnerText;

}



Post یه متیغر رشته ای هست (چون از پروژه خودم کپی کردم منظم تر از این نمی شد ) ولی همه چیز واضح هست

mr_ayma
چهارشنبه 15 مهر 1394, 14:03 عصر
یک مزیتی که کد بالا داره اینکه ، چون UserAgent موزیلا هست معمولا برای اکثر سرورها بدون مشکل جواب می ده ، منتهی بهتر از این روش اینکه با RSS سایت مقصد رو با جی کوئری بخونید