Advertisement
andruhovski

Task 982: Selectors API Demo

Oct 9th, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.89 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Aspose.Html;
  4. using Aspose.Html.Collections;
  5. using Aspose.Html.Dom;
  6. using Aspose.Html.Dom.Traversal;
  7. using Aspose.Html.Dom.Traversal.Filters;
  8.  
  9. namespace Task982
  10. {
  11.     class Program
  12.     {
  13.         private static HTMLDocument _document;
  14.  
  15.         private static string Tabs(int n)
  16.         {
  17.             return new string('\t', n);
  18.         }
  19.         static void Main(string[] args)
  20.         {
  21.             const string url = @"http://asposedemo20170904120448.azurewebsites.net/home/QuerySelector";            
  22.             try
  23.             {
  24.                 _document = new HTMLDocument(url);
  25.             }
  26.             catch (Exception e)
  27.             {
  28.                 Console.WriteLine(e);
  29.                 return;
  30.             }
  31.             Demo01();
  32.             Demo02();
  33.             Demo03();
  34.             Demo04();
  35.             Demo05();
  36.             Demo06();
  37.         }
  38.  
  39.         private static void Demo01()
  40.         {
  41.             var elementById = _document.GetElementById("Listfriends");
  42.             var elementBySelector = _document.QuerySelector("#Listfriends");
  43.             Console.WriteLine($"Listfriends is {elementById.TagName}");
  44.             Console.WriteLine($"Listfriends is {elementBySelector.TagName}");
  45.         }
  46.  
  47.         private static void Demo02()
  48.         {
  49.             var inputs = _document.GetElementsByTagName("input");
  50.             Console.WriteLine($"We have a {inputs.Length} inputs");
  51.             //var radiosGroup = _document.GetElementsByName("rr");
  52.             //Console.WriteLine($"We have {radiosGroup.Length} radio");
  53.             var unorderedList = _document.QuerySelectorAll("ul");
  54.             Console.WriteLine($"We have a {unorderedList.Length} ULs");
  55.         }
  56.  
  57.         private static void Demo03()
  58.         {
  59.             var links = _document.Links;
  60.             Console.WriteLine($"We have a {links.Length} links.");
  61.             var forms = _document.Forms;            
  62.             Console.WriteLine($"We have a {forms.Length} forms.");
  63.         }
  64.  
  65.         private static void Demo04()
  66.         {
  67.             var elementById = _document.GetElementById("Listfriends");
  68.             var elementsByClassName = _document.GetElementsByClassName("intro").First();
  69.             var elementsByTagName = _document.GetElementsByTagName("h4").First();
  70.             Console.WriteLine($"Element found by Id:{elementById.TagName}"); //UL
  71.             Console.WriteLine($"First element found by class name:{elementsByClassName.TagName}"); //DIV
  72.             Console.WriteLine($"First element found by tag name:{elementsByTagName.TagName}"); //H4
  73.         }
  74.  
  75.         private static void Demo05()
  76.         {
  77.             var element1 = _document.GetElementById("Listfriends");
  78.             element1.SetAttribute("class", "newsletter");
  79.  
  80.             var element2 = _document.QuerySelector("#Listfriends");
  81.             element2.SetAttribute("class", "newsletter");
  82.  
  83.             var elements3 = _document.GetElementsByTagName("p");
  84.             foreach (var element in elements3)
  85.             {
  86.                 element.SetAttribute("class", "newsletter");
  87.             }
  88.             var elements4 = _document.QuerySelectorAll("p");
  89.             foreach (var element in elements4)
  90.             {
  91.                 element.SetAttribute("class", "newsletter");
  92.             }
  93.         }
  94.  
  95.         private static void Demo06()
  96.         {
  97.             // Select by matching 'class' attributes.intro
  98.             var element3 = _document.QuerySelector(".intro");
  99.             Console.WriteLine(element3.TagName); // DIV
  100.  
  101.             //Select by matching 'id' attributes Lastname
  102.             var element4 = _document.QuerySelector("#Lastname");
  103.             Console.WriteLine(element4.TagName); //SPAN
  104.  
  105.             //Select by finding direct children
  106.             var collection5 = _document.QuerySelectorAll("ul>li");
  107.             Console.WriteLine(collection5.Length); //4
  108.  
  109.             //Descendent Select by finding all decedents div p
  110.             var collection6 = _document.QuerySelectorAll("div p");
  111.             Console.WriteLine(collection6.Length); //4
  112.  
  113.             //Sibling Selects nearest next sibling h1+p
  114.             var collection7 = _document.QuerySelectorAll("ul+h3");
  115.             Console.WriteLine(collection7.Length); //1
  116.  
  117.             //Attribute Selects based on an attribute and or its value  input[type = 'button']
  118.             var collection8 = _document.QuerySelectorAll("input[type = 'checkbox']");
  119.             foreach (var element in collection8)
  120.             {
  121.                 Console.WriteLine(element.Id); //cc1 cc2 cc3
  122.             }
  123.             Console.WriteLine(collection8.Length); //3
  124.  
  125.             //Selects based on a CSS selector: psuedo class ul : first-child
  126.             var collection9 = _document.QuerySelector("p:first-child");
  127.             Console.WriteLine(collection9.TextContent); // My name is Donald Duck
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement