Skip to content

Instantly share code, notes, and snippets.

@parsalotfy
Last active February 26, 2020 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parsalotfy/7fe4ac1202a5d53e3a33a0e6d44d1839 to your computer and use it in GitHub Desktop.
Save parsalotfy/7fe4ac1202a5d53e3a33a0e6d44d1839 to your computer and use it in GitHub Desktop.
A Test for Html Aglility Pack Encapsulator Tool
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
namespace DotNet20_Test
{
static class Program
{
static void Main(string[] args)
{
HtmlWeb stackoverflowSite = new HtmlWeb();
HtmlDocument htmlDocument = stackoverflowSite.Load("https://stackoverflow.com/questions");
StackOverflowPage stackOverflowPage = htmlDocument.DocumentNode.GetEncapsulatedData<StackOverflowPage>();
Console.ReadKey();
}
}
[HasXPath]
public class StackOverflowPage
{
[XPath("//*[@id='questions']/div")]
public IEnumerable<StackOverflowQuestion> Questions { get; set; }
[XPath("//*[@id='hot-network-questions']/ul//li")]
public IEnumerable<HotNetworkQuestion> GetHotNetworkQuestions { get; set; }
}
[HasXPath]
[DebuggerDisplay("StackOverflowQuestion : {Question.QuestionTitle}")]
public class StackOverflowQuestion
{
[XPath("/div[@class='statscontainer']")]
public StatisticsBox Statistics { get; set; }
[XPath("/div[@class='summary']")]
public QuestionBox Question { get; set; }
[XPath("/div[@class='summary']/div[@class='started fr']/div")]
public UserBox User { get; set; }
}
[HasXPath]
[DebuggerDisplay("Votes={Votes} , Answers={Answers} , Views={Views}")]
public class StatisticsBox
{
[XPath("/div[1]/div[1]/div/span/strong")]
public int Votes { get; set; }
[XPath("/div[1]/div[2]/strong")]
public int Answers { get; set; }
[XPath("/div[2]")]
public string Views { get; set; }
}
[HasXPath]
[DebuggerDisplay("QuestionTitle={QuestionTitle}")]
public class QuestionBox
{
[XPath("/h3/a")]
public string QuestionTitle { get; set; }
[XPath("/h3/a", "href")]
public string QuestionLink { get; set; }
[XPath("/div[starts-with(@class,'tags')]//a")]
public IEnumerable<string> Tags { get; set; }
}
[HasXPath]
[DebuggerDisplay("UserID={UserID} , ReputationScore={ReputationScore}")]
public class UserBox
{
[XPath("/div[@class='user-action-time']/span", "title")]
public DateTime ExactTime { get; set; }
[XPath("/div[@class='user-action-time']/span")]
public string RelativeTime { get; set; }
[XPath("/div[@class='user-details']/a","href")]
public string UserLink { get; set; }
[XPath("/div[@class='user-details']/a")]
public string UserName { get; set; }
[XPath("/div[@class='user-details']/div[1]")]
public string ReputationScore { get; set; }
}
[HasXPath]
[DebuggerDisplay("Question Title={QuestionTitle}")]
public class HotNetworkQuestion
{
[XPath("/div", "title")]
public string QuestionCategory { get; set; }
[XPath("/a")]
public string QuestionTitle { get; set; }
[XPath("/a", "href")]
public string QuestionLink { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment