HtmlAgility:no contents appeared (C#,UWP)

c# html html-agility-pack uwp win-universal-app

Question

I tried to use htmlagilitypack to parse a table, but after I finished, I realized that I had forgotten to test whether the htmlagility part worked or not. Although it is obvious that this didn't work, I wasn't sure what I had missed or where I had gone completely wrong. Therefore, please don't be too harsh with me.

public partial class WebForm1 : System.Net.Http.HttpClient
{
    protected void Page_Load(object sender, EventArgs e)
    {

        System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();

        string header = "ie";
        if (!headers.UserAgent.TryParseAdd(header))
        {
            throw new Exception("Invalid header value: " + header);
        }

        header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
        if (!headers.UserAgent.TryParseAdd(header))
        {
            throw new Exception("Invalid header value: " + header);
        }

        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();


        htmlDoc.LoadHtml(" http://www.eurogymnasium-waldenburg.de/egw_content/Stunden_Vertretungsplan/home.html");



        HtmlNode docNodes = htmlDoc.DocumentNode;

        HtmlNode navNode = htmlDoc.GetElementbyId("bereichaktionen");

        HtmlNode docNode = htmlDoc.DocumentNode.SelectSingleNode("/html/body[@class='ui-widget']/div[@id='main']/div[@id='vplan']/div[@id='bereichaktionen']");

        string nodeValue;

        nodeValue = (docNode.InnerText);

        Debug.WriteLine("nodeValue");

/ I don't think anything is incorrect above, but I'm not certain either.

        if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
        {

        }
        else
        {

            if (htmlDoc.DocumentNode != null)
            {
                HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");

                if (bodyNode != null)
                {

                }
            }
        }
    }

You guys might give the origin url a try.

I appreciate it X.L

1
1
6/18/2016 12:18:32 PM

Popular Answer

First off, the universal app does not support the third-party package Agility Pack for HTML that you are now using. Please utilize the universal app's supported 1.4.9.2 HtmlAgilityPack for.NET Core.

Second, the method's argumenthtmlDoc.LoadHtml(string html) is the HTML content that may be obtained from a webrequest's response, not the Uri of the HTML site.

Therefore, the proper code should be as follows:

WebRequest request = HttpWebRequest.Create("http://www.eurogymnasium-waldenburg.de/egw_content/Stunden_Vertretungsplan/home.html");
WebResponse response = await request.GetResponseAsync();
Stream stream = response.GetResponseStream();
var result = "";
using (StreamReader sr = new StreamReader(stream))
{
    result = sr.ReadToEnd();
}
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(result);
var node = htmlDoc.DocumentNode.SelectSingleNode("/html/body[@class='ui-widget']/div[@id='main']/div[@id='vplan']/div[@id='bereichaktionen']");

Additionally, I publish the whole CHtmlAgility project to github so that you may download it and test it.

4
12/7/2017 3:12:30 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow