Reading non-html content with AgilityPack

c# html-agility-pack

Question

One of services we are loading is responding just with pure JSON object. We are loading all services with html agility pack, all but this. Other services are rendering a script tag, with a JSON within, and it works as expected. But I am not able to load this data, when it comes in non-html format. Loaded document has no elements, and Text property is an empty string, DocumentElement's outer/inner html throws object null exception, innerText is empty or null.

I try to load this one service with HttpWebRequest and it does the job, but I don't want to mix technologies just because this service.

Is it possible load pure JSON data page with HtmlAgilityPack?

Accepted Answer

UPDATE :

Turned out that I've misunderstood the question.

Core functionality of HAP is for parsing HTML, while your problem is in downloading the HTML (or JSON in this case). HAP's HtmlWeb only provide basic functionality to perform this task, so you're very likely have to switch to other tools once you find yourself in a situation where HtmlWeb is no longer working. This is another example of this kind of situation : HTML Agility Pack settings


Initial Answer :

Quick test shows that DocumentElement.InnerText returns the JSON just fine :

var json = @"{
    identifier: '2051189775',     //PRODUCT ID
    fn: 'Fit- Whiskered Dark Wash Skirt',
    category: ['sale'],
    brand: 'Brand Name',
    price: '22.90',  // this would be the discount price
    amount: '31.80',  // this would be the original price
    currency: 'USD',
    //List can me even more.
};";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(json);

Console.WriteLine(doc.DocumentNode.InnerText);

Live demo here : https://dotnetfiddle.net/nPT49L

If this is not working for you, please post sample JSON data that will demonstrate the problem.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why