1

I googled my problem and found Html Agility Pack to parse html in c#. But there is no good examples and I can't use it to my purpose. I have a html document and it has a part like this:

<div class="pray-times-holder">
    <div class="pray-time">
        <div class="labels">
            Time1:</div>
        04:28:24
    </div>
    <div class="pray-time">
        <div class="labels">
            Time2:</div>
        06:04:41
    </div>
</div>

I want to get the value for Time1 and Time2. e.g. Time1 has value 04:28:24 and Time2 has value 06:04:41 and I want to get these values. Can you help me please?

2
  • How familiar are you with the XmlDocument class and XPaths? The HAP has a very similar interface.
    – Cameron
    Apr 30, 2012 at 5:12
  • @Cameron no unfortunately i'm not familiar with xpath. can you help me please?
    – amiry jd
    Apr 30, 2012 at 5:17

2 Answers 2

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

var results = doc.DocumentNode
    .Descendants("div")
    .Where(n => n.Attributes["class"] != null && n.Attributes["class"].Value == "pray-time")
    .Select(n => n.InnerText.Replace("\r\n","").Trim())
    .ToArray();
1

This console application code:

 HtmlDocument doc = new HtmlDocument();
 doc.Load(yourHtml);
 foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[@class = 'labels']"))
 {
     Console.WriteLine(node.NextSibling.InnerText.Trim());
 }

will output this:

04:28:24
06:04:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.