Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using Aspose.Html;
- using Aspose.Html.Collections;
- using Aspose.Html.Dom;
- using Aspose.Html.Dom.Traversal;
- using Aspose.Html.Dom.Traversal.Filters;
- namespace Task982
- {
- class Program
- {
- private static HTMLDocument _document;
- private static string Tabs(int n)
- {
- return new string('\t', n);
- }
- static void Main(string[] args)
- {
- const string url = @"http://asposedemo20170904120448.azurewebsites.net/home/QuerySelector";
- try
- {
- _document = new HTMLDocument(url);
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- return;
- }
- Demo01();
- Demo02();
- Demo03();
- Demo04();
- Demo05();
- Demo06();
- }
- private static void Demo01()
- {
- var elementById = _document.GetElementById("Listfriends");
- var elementBySelector = _document.QuerySelector("#Listfriends");
- Console.WriteLine($"Listfriends is {elementById.TagName}");
- Console.WriteLine($"Listfriends is {elementBySelector.TagName}");
- }
- private static void Demo02()
- {
- var inputs = _document.GetElementsByTagName("input");
- Console.WriteLine($"We have a {inputs.Length} inputs");
- //var radiosGroup = _document.GetElementsByName("rr");
- //Console.WriteLine($"We have {radiosGroup.Length} radio");
- var unorderedList = _document.QuerySelectorAll("ul");
- Console.WriteLine($"We have a {unorderedList.Length} ULs");
- }
- private static void Demo03()
- {
- var links = _document.Links;
- Console.WriteLine($"We have a {links.Length} links.");
- var forms = _document.Forms;
- Console.WriteLine($"We have a {forms.Length} forms.");
- }
- private static void Demo04()
- {
- var elementById = _document.GetElementById("Listfriends");
- var elementsByClassName = _document.GetElementsByClassName("intro").First();
- var elementsByTagName = _document.GetElementsByTagName("h4").First();
- Console.WriteLine($"Element found by Id:{elementById.TagName}"); //UL
- Console.WriteLine($"First element found by class name:{elementsByClassName.TagName}"); //DIV
- Console.WriteLine($"First element found by tag name:{elementsByTagName.TagName}"); //H4
- }
- private static void Demo05()
- {
- var element1 = _document.GetElementById("Listfriends");
- element1.SetAttribute("class", "newsletter");
- var element2 = _document.QuerySelector("#Listfriends");
- element2.SetAttribute("class", "newsletter");
- var elements3 = _document.GetElementsByTagName("p");
- foreach (var element in elements3)
- {
- element.SetAttribute("class", "newsletter");
- }
- var elements4 = _document.QuerySelectorAll("p");
- foreach (var element in elements4)
- {
- element.SetAttribute("class", "newsletter");
- }
- }
- private static void Demo06()
- {
- // Select by matching 'class' attributes.intro
- var element3 = _document.QuerySelector(".intro");
- Console.WriteLine(element3.TagName); // DIV
- //Select by matching 'id' attributes Lastname
- var element4 = _document.QuerySelector("#Lastname");
- Console.WriteLine(element4.TagName); //SPAN
- //Select by finding direct children
- var collection5 = _document.QuerySelectorAll("ul>li");
- Console.WriteLine(collection5.Length); //4
- //Descendent Select by finding all decedents div p
- var collection6 = _document.QuerySelectorAll("div p");
- Console.WriteLine(collection6.Length); //4
- //Sibling Selects nearest next sibling h1+p
- var collection7 = _document.QuerySelectorAll("ul+h3");
- Console.WriteLine(collection7.Length); //1
- //Attribute Selects based on an attribute and or its value input[type = 'button']
- var collection8 = _document.QuerySelectorAll("input[type = 'checkbox']");
- foreach (var element in collection8)
- {
- Console.WriteLine(element.Id); //cc1 cc2 cc3
- }
- Console.WriteLine(collection8.Length); //3
- //Selects based on a CSS selector: psuedo class ul : first-child
- var collection9 = _document.QuerySelector("p:first-child");
- Console.WriteLine(collection9.TextContent); // My name is Donald Duck
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement