Skip to content Skip to sidebar Skip to footer

Getting Li Values From Multiple Ul's Using HtmlAgilityPack C#

This query works perfect for some countries like Germany '//h2[span/@id='Cities' or span/@id='Other_destinations']' + '/following-sibling::ul[1]' + '/li'; Where the HTML is format

Solution 1:

I hope this code solve your problem :

var xpath = "//ul[preceding-sibling::h2[span/@id='Cities' or span/@id='Other_destinations'] and following-sibling::h2[span/@id='Get_in']]" + "/li";

var doc = new HtmlDocument
{
   OptionDefaultStreamEncoding = Encoding.UTF8
};

// You need to call a WebClient here and set to the html variable.    
var html = String.Empty;

doc.LoadHtml(html);

using (var write = new StreamWriter("testText.txt"))
{
    foreach (var node in doc.DocumentNode.SelectNodes(xpath))
    {
        var all = node.InnerText;

        //Writes to text file
        write.WriteLine(all);
     }
 }        

The above XPath can be translated to :

  • Select all the ul tags has between by a h2[span/@id='Cities' or span/@id='Other_destinations'] and a h2[span/@id='Get_in']]

I see that in all the pages has a span tag with id='Get_in' in the final.

I hope it solve your problem.


Post a Comment for "Getting Li Values From Multiple Ul's Using HtmlAgilityPack C#"