Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the code is given below

string rssft = "http://www.ft.com/rss/home/us";
var docft = XDocument.Load(rssft);


in the above code docft contains null value and the error occurred The remote server returned an error: (403) Forbidden.

pls help me

Thanks in advance
Posted
Comments
Andreas Gieriet 8-Oct-15 5:51am    
No re-post, please!

1 solution

Hi,


There is no overload with URL in XDocument.Load : https://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.load(v=vs.110).aspx[^]

you should do :

C#
var m_URL = "http://www.ft.com/rss/home/us"; // Set your URL
string xmlStr;
using(var wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
    xmlStr = wc.DownloadString(m_URL);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
 
Share this answer
 
v3
Comments
Athul MS 8-Oct-15 0:51am    
"http://www.ft.com/rss/home/us";
the above line occurred an error ie,semicolon expected(;)
Suvabrata Roy 8-Oct-15 0:58am    
Try above Code
Athul MS 8-Oct-15 0:58am    
in the above code xmlStr conains null value.the same error occurred The remote server returned an error: (403) Forbidden
Suvabrata Roy 8-Oct-15 1:03am    
Now try it, they have some check on user agent.
Athul MS 8-Oct-15 1:38am    
its working fine but i need to get the elements from the item tag
the code is given below

var m_URL = "http://www.ft.com/rss/home/us"; // Set your URL
string xmlStr;
using (var wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
xmlStr = wc.DownloadString(m_URL);
}
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);



foreach (var el in xmlDoc.Elements("rss").Elements("channel").Elements("item")) //here the error "elements" does not exist in the xmldoc
{
var titl = el.Element("title").Value;

var desc = el.Element("description").Value;
var date = el.Element("pubDate").Value;
var lnk = el.Element("link").Value;
lstnews.Add(new NewsFeed
{
title = titl,
description = desc,
section = "ft",
date = Convert.ToDateTime(date),
link = lnk
});
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900