XDocument Load - cannot open - c#

I'm trying to load rss feed by XDocument.
The url is:
http://www.ft.com/rss/home/uk
XDocument doc = XDocument.Load(url);
But I'm getting an error:
Cannot open 'http://www.ft.com/rss/home/uk'. The Uri parameter must be a file system relative or absolute path.

XDocument.Load does not take URL's, only files as stated in the documentation.
Try something like the following code which I totally did not test:
using(var httpclient = new HttpClient())
{
var response = await httpclient.GetAsync("http://www.ft.com/rss/home/uk");
var xDoc = XDocument.Load(await response.Content.ReadAsStreamAsync());
}

Related

XML from URL - Data at the root level is invalid. Line 1, position 1 Why it works with one URL and not the other?

As far as I can tell, these two end points are both valid XML output. However when I use the same code on the second end point I get the error:
Data at the root level is invalid. Line 1, position 1
Here is my code:
//Works
XmlDocument testDocument = new XmlDocument();
testDocument.Load("https://www.w3schools.com/xml/note.xml");
//Fails
XmlDocument testDocumentTwo = new XmlDocument();
testDocumentTwo.Load("https://www.domainNameHere.com/direct/umbraco/api/productsearch/NameSearch?countryCode=en-gb");
I opened Fiddler and watched the request and its response, and lo and behold your endpoint is returning JSON, not XML:
If I use HttpClient to set an explicit Accept header, then I get XML back and everything works:
using var client = new HttpClient();
var requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://www.clinigengroup.com/direct/umbraco/api/productsearch/NameSearch?countryCode=en-gb");
requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var response = await client.SendAsync(requestMessage);
var xml = await response.Content.ReadAsStringAsync();
XmlDocument testDocumentTwo = new XmlDocument();
testDocumentTwo.LoadXml(xml);

C# HTMLAgilityPack getting src. xpath is not valid

I am not able to get the xpath right. I am trying to get the image of any IMDB movie but it just seems not to work. This is my code of it.
// Getting the node
HtmlNode node = doc.DocumentNode.SelectSingleNode("//*[#id=\"title - overview - widget\"]/div[2]/div[3]/div[1]/a/img");
// Getting the attribute data
HtmlAttributeCollection attr = node.Attributes;
the attribute is null. every time but. the xpath does not work and i dont know why. it seems good to me.
You can use a simpler xpath
var url = "http://www.imdb.com/title/tt0816692/";
using (var client = new HttpClient())
{
var html = await client.GetStringAsync(url);
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
var img = doc.DocumentNode.SelectSingleNode("//img[#title='Trailer']")
?.Attributes["src"]?.Value;
//or
var poster = doc.DocumentNode.SelectSingleNode("//div[#class='poster']//img")
?.Attributes["src"]?.Value;
}

Web API Request Data error on second time

Strange Error.
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.Request.Content.ReadAsStreamAsync().Result);
var xmlDoc1 = new System.Xml.XmlDocument();
xmlDoc1.Load(this.Request.Content.ReadAsStreamAsync().Result);
In WEB API, I try to load the POST data in to xmlXoc it is working good
When I try to load it again in to xmlDoc1 (new variable), I am getting a Root Element missing error.
I see that ReadAsStreamAsync is a Read-Only-Stream but why the error on the last line ?
Save the Stream in a local variable and reset it to the beginning when reading it a second time.
var stream = this.Request.Content.ReadAsStreamAsync().Result
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(stream);
// RESET
stream.Position = 0;
var xmlDoc1 = new System.Xml.XmlDocument();
xmlDoc1.Load(stream);

Why XDocument.Load(url) throws exception?

I am new to C# and I am trying to read xml from URL.
xml looks like this
<posts>
<post>
<title>title1</title>
<des>des1</des>
</post>
<post>
<title>title2</title>
<des>des2</des>
</post>
.....
</posts>
And this is what I am using to parse it.
String uri = "url";
XDocument books = XDocument.Load(uri);
When the debug hits XDocument line it throws an exception and skips it.
How can I avoid this?
I think URI for your XML is lacking the extension of the file which is causing the problem. Please try using:
String uri = PATH + "url.xml";
XDocument books = new XDocument();
books.Load(uri);
To parse XML obtained from URL u can use:
string strURL = "http://<some-server>/<some-uri-path>";
string xmlStr;
WebClient wc = new WebClient();
xmlStr = wc.DownloadString(strURL);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);

C# consume rss feed containing xml-stylesheet?

I have a problem for parsing a rss feed using c#.
I used to use this method to load the feed.
XDocument rssFeed = XDocument.Load(#url);
But, when I notice when the feed has a xml-stylesheet this method crashes saying the xml is not well formated...
Here's a rss feed that contains this tag
http://www.channelnews.fr/accueil.feed?type=rss
What would be the best way to parse any rss feed using c#?
Thanks for your help
This code works for me
static XDocument DownloadPage()
{
var req = (HttpWebRequest)WebRequest.Create("http://www.channelnews.fr/accueil.feed?type=rss");
req.UserAgent = "Mozilla";
using(var response = req.GetResponse())
using(var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
return XDocument.Load(reader);
}
Note, that if you omit setting UserAgent, then response will contain string 'DOS' that is defnintly not xml :)
This one works nicer:
XDocument xdoc = XDocument.Load("http://pedroliska.wordpress.com/feed/");
var items = from i in xdoc.Descendants("item")
select new
{
Title = i.Element("title").Value
};
So now you can access the rss titles by doing a loop or something like:
items[0].Title
And just the code is pulling the title from the rss feed, you can pull the description, link, pubDate, etc.

Categories

Resources