Guest User

Untitled

a guest
Feb 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HtmlAgilityPack
  8. {
  9. internal static class HtmlHelper
  10. {
  11. public static HtmlNode Element(this HtmlNode node, string name, string className)
  12. {
  13. return node.Elements(name, className).FirstOrDefault();
  14. }
  15.  
  16. public static IEnumerable<HtmlNode> Elements(this HtmlNode node, string name, string className)
  17. {
  18. if (node == null)
  19. throw new ArgumentNullException(nameof(node));
  20. return node.Elements(name).Where(n => n.HasClass(className));
  21. }
  22.  
  23. public static string GetAttribute(this HtmlNode node, string attributeName, string def = null)
  24. {
  25. if (node == null)
  26. throw new ArgumentNullException(nameof(node));
  27. return HtmlEntity.DeEntitize(node.GetAttributeValue(attributeName, null)) ?? def;
  28. }
  29.  
  30. public static int GetAttribute(this HtmlNode node, string attributeName, int def = default)
  31. {
  32. var r = node.GetAttribute(attributeName, default(string));
  33. if (r == null)
  34. return def;
  35. if (int.TryParse(r, out var intP))
  36. return intP;
  37. return def;
  38. }
  39.  
  40. public static bool GetAttribute(this HtmlNode node, string attributeName, bool def = default)
  41. {
  42. var r = node.GetAttribute(attributeName, default(string));
  43. if (r == null)
  44. return def;
  45. return true;
  46. }
  47.  
  48. public static Uri GetAttribute(this HtmlNode node, string attributeName, Uri baseUri, Uri def = default)
  49. {
  50. var r = node.GetAttribute(attributeName, default(string));
  51. if (r == null)
  52. return def;
  53. return new Uri(baseUri, r);
  54. }
  55.  
  56. public static T GetAttribute<T>(this HtmlNode node, string attributeName, T def = default)
  57. {
  58. var r = node.GetAttribute(attributeName, default(string));
  59. if (r == null)
  60. return def;
  61. return (T)Convert.ChangeType(r, typeof(T));
  62. }
  63.  
  64. public static string GetInnerText(this HtmlNode node)
  65. {
  66. if (node == null)
  67. throw new ArgumentNullException(nameof(node));
  68. return HtmlEntity.DeEntitize(node.InnerText);
  69. }
  70. }
  71. }
Add Comment
Please, Sign In to add comment