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.HtmlDocumentdoc=newHtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

varoptions= 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 dictionaryforeach (HtmlNode city in citiesNodes)
        {
            if (city.NextSibling != null)
            {
                string key   = city.NextSibling.InnerText;
                stringvalue = 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