Skip to content Skip to sidebar Skip to footer

How Can I Parse InnerText Of

Context: I am trying to parse the 'Cities' from this Page here. I already managed to simulate the request for the data of this combobox, which is a Ajax call. Fiddler Request : PO

Solution 1:

Just use HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("option"); before loading html

HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("option");

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var options = doc.DocumentNode.Descendants("option").Skip(1)
                .Select(n => new
                {
                    Value = n.Attributes["value"].Value,
                    Text = n.InnerText
                })
                .ToList();

Solution 2:

For some weird reason, HtmlAgilityPack does not handles those tags correctly, so this managed to solve my problem.

        // Iterating over nodes to build the dictionary
        foreach (HtmlNode city in citiesNodes)
        {
            if (city.NextSibling != null)
            {
                string key   = city.NextSibling.InnerText;
                string value = city.Attributes["value"].Value;

                citiesHash.AddCity (key,value);
            }
        }

Instead of reaching directly the node,i managed to get the values of each node by using the NextSimbling reference from the previous simbling.


Post a Comment for "How Can I Parse InnerText Of