This article is a mirror article of machine translation, please click here to jump to the original article.

View: 31367|Reply: 4

[Source] Html Agility Pack Download and How to Use It

[Copy link]
Posted on 8/4/2016 5:05:36 PM | | | |
Download Address:

http://htmlagilitypack.codeplex.com/

IEnumerable<HtmlNode> Ancestors();               Returns a collection of all parent nodes of this element.
IEnumerable<HtmlNode> Ancestors(string name);                         Returns a collection of all parent nodes whose parameter names match this element.
IEnumerable<HtmlNode> AncestorsAndSelf();                          Returns all parent nodes and its own collection of this element.
IEnumerable<HtmlNode> AncestorsAndSelf(string name);                    Returns a collection of all parent nodes and itself that match the name of this element.
HtmlNode AppendChild(HtmlNode newChild);                            Appends a parameter element to a child element that is calling the element (appended at the end)
void AppendChildren(HtmlNodeCollection newChildren);              Appends elements in the parameter collection as children of the calling element (appended at the end)
HtmlNode PrependChild(HtmlNode newChild);                     Place the element in the argument as a child element at the top of the calling element
void PrependChildren(HtmlNodeCollection newChildren);              Place all elements in the parameter set as child elements, before the calling element
static bool CanOverlapElement(string name);                    Determine if duplicate elements can be saved
IEnumerable<HtmlAttribute> ChildAttributes(string name);            Get the attributes of all child elements (parameter names should match the element names)
HtmlNode Clone();                                        This node is cloned to a new node
HtmlNode CloneNode(bool deep);                         The node is cloned to a new point, and the parameters determine whether to clone the child elements together
HtmlNode CloneNode(string newName);                      Change the element name while cloning
HtmlNode CloneNode(string newName, bool deep);          Change the element name while cloning. parameter to determine whether to clone together with the child elements
void CopyFrom(HtmlNode node);                          Create duplicate nodes and subtrees under them.
void CopyFrom(HtmlNode node, bool deep);                    Create a copy of the node.
XPathNavigator CreateNavigator();                                One returned for this document for XPathNavigator
static HtmlNode CreateNode(string html);                      Static method that allows creating a new node with a string
XPathNavigator CreateRootNavigator();                      Create an XPathNavigator for the root path
IEnumerable<HtmlNode> DescendantNodes();                          Get all child nodes
IEnumerable<HtmlNode> DescendantNodesAndSelf();                      Get all the child nodes and yourself
IEnumerable<HtmlNode> Descendants();                     Get all the child nodes in the enumeration list
IEnumerable<HtmlNode> Descendants(string name);               Get all the child nodes in the enumeration list, and note that the element names match the parameters
IEnumerable<HtmlNode> DescendantsAndSelf();                 Get all the child nodes in the enumeration list as well as themselves
IEnumerable<HtmlNode> DescendantsAndSelf(string name);    Get all the child nodes in the enumeration list and themselves, and pay attention to the element names to match the parameters
HtmlNode Element(string name);                          Get an element based on the parameter name
IEnumerable<HtmlNode> Elements(string name);                 Get a matching set of elements based on the parameter name
bool GetAttributeValue(string name, bool def);                   A help method used to obtain the value of the property of this node (Boolean type). If the property is not found, the default value is returned.
int GetAttributeValue(string name, int def);                     The help method is used to get the value (integer) of the attribute of this node. If the property is not found, the default value is returned.
string GetAttributeValue(string name, string def);                 The help method is used to get the value (string type) of the property of this node. If the property is not found, the default value is returned.
HtmlNode InsertAfter(HtmlNode newChild, HtmlNode refChild);            Inserting a node behind the second parameter node is a sibling relationship with the second parameter
HtmlNode InsertBefore(HtmlNode newChild, HtmlNode refChild);          It is said that a node is inserted after the second parameter node, and the second parameter is a sibling relationship
static bool IsCDataElement(string name);                      Determines whether an element node is a CDATA element node.
static bool IsClosedElement(string name);                     Determines whether the element node is closed or not
static bool IsEmptyElement(string name);                       Determines whether an empty element node is present.
static bool IsOverlappedClosingElement(string text);                 Determining whether the text corresponds to a node can preserve overlapping end markers.
void Remove();                                   Remove the calling node from the parent collection
void RemoveAll();                                  Remove all child nodes and properties of the calling node
void RemoveAllChildren();                              Remove all child nodes of the calling node
HtmlNode RemoveChild(HtmlNode oldChild);                     Remove the child node with the specified name of the calling node
HtmlNode RemoveChild(HtmlNode oldChild, bool keepGrandChildren); Remove the child node of the calling node's call name, and the second parameter determines whether to remove it along with the grandchild node
HtmlNode ReplaceChild(HtmlNode newChild, HtmlNode oldChild);          Replace one of the original child nodes of the calling node with a new node, and the second parameter is the old node
HtmlNodeCollection SelectNodes(string xpath);           Get a collection of nodes based on XPath
HtmlNode SelectSingleNode(string xpath);                     Get a unique node based on XPath
HtmlAttribute SetAttributeValue(string name, string value);             Set the properties of the calling node
string WriteContentTo();                               Save all the children of that node to a string.
void WriteContentTo(TextWriter outText);                     Save all children of that node to the specified TextWriter.
string WriteTo();                                  Save the current node to a string.
void WriteTo(TextWriter outText);                         Save the current node to the specified TextWriter.
void WriteTo(XmlWriter writer);                            Save the current node to the specified XmlWriter.





Previous:HtmlAgilityPack parses html5's xpath node with an error
Next:ASP.NET MVC modifies the Shared default path of the view (MVC2, MVC3)
 Landlord| Posted on 8/5/2016 2:45:10 PM |


Gets the value of the node attribute
 Landlord| Posted on 3/1/2017 5:16:09 PM |

XPath path expression

Used to select nodes or sets of nodes in an XML document

1. Terminology: Node: 7 types: elements, attributes, text, namespaces, processing commands, comments, document (root) nodes

2. Node relationships: Parent, Children, Sibling, Ancestor, Descendant

3. Path expression

nodename node name, select all child nodes of this node Example: childnode The childnode child node in the current node does not include the nodes of grandchildren and below

/     从根节点选取  例:/root/childnode/grandsonnode  

  Represents all descendant nodes e.g. ://childnode All descendant nodes named childnodes

.    Represents the current node Example: ./childnode represents the childnode of the current node

..     Represent the parent node Example: /nearnode represents the nearnode child node of the parent node

       @ Choose the attribute /root/childnode/@id to represent all node sets with id attributes in childnode

4. Predicates

Predicates can impose some restrictions on the node set, making the selection more precise

/root/book[1]    节点集中的第一个节点

/root/book[last()]  节点集中最后一个节点

/root/book[position() - 1]  节点集中倒数第二个节点集

/root/book[position() < 5]  节点集中前五个节点集

/root/book[@id]      节点集中含有属性id的节点集

/root/book[@id='chinese']  节点集中id属性值为chinese的节点集

/root/book[price > 35]/title  节点集中book的price元素值大于35的title节点集

5. Wildcards: Wildcards (*,@*,node(), text()) are also supported in the XPath path.

Example: /bookstore/*

//title[@*]

6. XPath axis

Define a set of nodes relative to the current node

ancestor all ancestor nodes

attribute all attribute nodes

child all child elements

descendant all descendant nodes (sons, grandchildren...). )

following all nodes after ending marking preceding all nodes before starting tagging

following-sibling ends all sibling nodes after the tag

preceding-sibling all sibling nodes before starting tagging

namespace: all nodes in the current namespace

parent parent

self current node

Usage: Axis Name: Node Test [Predicate]

For example: ancestor::book

   child::text()

7. Operators

|  Merger of two node sets Example: /root/book[1] | /root/book[3]

+,-,*,dev,mod

=,!=,<,>,<=,>=

or, and or and
 Landlord| Posted on 1/4/2018 4:39:23 PM |
Take the value of the specific tag attribute var node = doc. DocumentNode.SelectSingleNode("//span[@prt-id='123']"). Attributes["tag"]. Value;
 Landlord| Posted on 4/16/2018 1:46:02 PM |


If I want to get the data of the table, the code is as follows:



The renderings are as follows:


Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com