HtmlAgilityPack官方文档(三)【Manipulation】

来源:互联网 发布:天刀人生至此知何似 编辑:程序博客网 时间:2024/06/16 05:05

遍历允许您遍历HTML节点。

属性

名称 描述 InnerHtml Gets or Sets the HTML between the start and end tags of the object. InnerText Gets the text between the start and end tags of the object. OuterHtml Gets the object and its content in HTML. ParentNode Gets the parent of this node (for nodes that can have parents).

方法

名称 描述 AppendChild() Adds the specified node to the end of the list of children of this node. AppendChildren() Adds the specified node to the end of the list of children of this node. Clone() Creates a duplicate of the node CloneNode(Boolean) Creates a duplicate of the node. CloneNode(String) Creates a duplicate of the node and changes its name at the same time. CloneNode(String, Boolean) Creates a duplicate of the node and changes its name at the same time. CopyFrom(HtmlNode) Creates a duplicate of the node and the subtree under it. CopyFrom(HtmlNode, Boolean) Creates a duplicate of the node. CreateNode() Creates an HTML node from a string representing literal HTML. InsertAfter() Inserts the specified node immediately after the specified reference node. InsertBefore Inserts the specified node immediately before the specified reference node. PrependChild Adds the specified node to the beginning of the list of children of this node. PrependChildren Adds the specified node list to the beginning of the list of children of this node. Remove Removes node from parent collection RemoveAll Removes all the children and/or attributes of the current node. RemoveAllChildren Removes all the children of the current node. RemoveChild(HtmlNode) Removes the specified child node. RemoveChild(HtmlNode, Boolean) Removes the specified child node. ReplaceChild() Replaces the child node oldChild with newChild node.

public virtual string InnerHtml { get; set; }

获取或设置对象的开始标记和结束标记之间的HTML。 InnerHtml是* HtmlAgilityPack.HtmlNode *的成员

例子

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//body/h1");foreach (var node in htmlNodes){    Console.WriteLine(node.InnerHtml);}

public virtual string InnerText { get; }

获取对象的开始和结束标记之间的文本。 InnerText是* HtmlAgilityPack.HtmlNode *的成员

例子

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//body/h1");foreach (var node in htmlNodes){    Console.WriteLine(node.InnerText);}

public virtual string OuterHtml { get; }

获取HTML中的对象及其内容。 OuterHtml是* HtmlAgilityPack.HtmlNode *的成员

例子

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//body/h1");foreach (var node in htmlNodes){    Console.WriteLine(node.OuterHtml);}

public HtmlNode ParentNode { get; }

获取此节点的父节点(对于可以有父节点的节点)。 ParentNode是* HtmlAgilityPack.HtmlNode *的成员

例子

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var node = htmlDoc.DocumentNode.SelectSingleNode("//body/h1");HtmlNode parentNode = node.ParentNode;Console.WriteLine(parentNode.Name);

public HtmlNode AppendChild(HtmlNode newChild)

将指定的节点添加到此节点的子节点列表的末尾。 AppendChild方法是* HtmlAgilityPack.HtmlNode *的成员

参数:

newChild: 要添加的节点。 不可能为null。

返回:

节点添加后的完整元素.

以下示例展示了追加子节点

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode h2Node = HtmlNode.CreateNode("<h2> This is h2 heading</h2>");htmlBody.AppendChild(h2Node);

public void AppendChildren(HtmlNodeCollection newChildren)

将指定的节点添加到此节点的子节点列表的末尾。 AppendChildren方法是* HtmlAgilityPack.HtmlNode *的成员

参数:

newChildren: 要添加的节点列表。不可能为null。

以下示例展示追加子节点列表。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode h2Node = HtmlNode.CreateNode("<h2> This is h2 heading</h2>");HtmlNode pNode1 = HtmlNode.CreateNode("<p> This is appended paragraph 1</p>");HtmlNode pNode2 = HtmlNode.CreateNode("<p> This is appended paragraph 2</p>");HtmlNodeCollection children = new HtmlNodeCollection(htmlBody);children.Add(h2Node);children.Add(pNode1);children.Add(pNode2);htmlBody.AppendChildren(children);

public HtmlNode Clone()

创建节点的副本。Clone方法是* HtmlAgilityPack.HtmlNode *的成员

返回:

节点的副本.

以下示例展示创建节点的副本

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode newHtmlBody = htmlBody.Clone();

public HtmlNode CloneNode(bool deep)

创建节点的副本。CloneNode方法是* HtmlAgilityPack.HtmlNode *的成员

参数:

deep: 为了递归地克隆指定节点下的子树; false只克隆节点本身。.

返回:

克隆的节点.

以下示例仅克隆没有子节点的节点本身。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode newHtmlBody = htmlBody.CloneNode(false);

public HtmlNode CloneNode(string newName)

创建节点的副本并同时更改其名称。 CloneNode方法是HtmlAgilityPack.HtmlNode的成员

参数:

newName: 克隆节点的新名称。 不可能为null。

返回:

克隆的节点

The following example clone the node and changes its name.
以下示例克隆该节点并更改其名称。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode h1Node = htmlBody.ChildNodes[1];HtmlNode h2Node = h1Node.CloneNode("h2");

public HtmlNode CloneNode(string newName, bool deep)

创建节点的副本并同时更改其名称。 CloneNode方法是HtmlAgilityPack.HtmlNode 的成员

参数:

newName: 克隆节点的新名称。 不可能为null。

deep: true 为了递归地克隆指定节点下的子树; false只克隆节点本身。

返回:

克隆的节点

以下示例仅克隆没有其子节点的节点本身,并更改其名称。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode h1Node = htmlBody.ChildNodes[1];HtmlNode h2Node = h1Node.CloneNode("h2");

public void CopyFrom(HtmlNode node)

创建节点和其下的子树的副本。 CopyFrom方法是HtmlAgilityPack.HtmlNode 的成员

参数:

node:要复制的节点。 不可能为null。

例子

以下示例复制节点及其下的子树。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode newBody = HtmlNode.CreateNode("<body></body>");newBody.CopyFrom(htmlBody);

public HtmlNode CopyFrom(HtmlNode node, bool deep)

创建节点的副本。 CopyFrom方法是HtmlAgilityPack.HtmlNode 的成员

参数:

node: 要复制的节点。 不可能为null。

deep:如果递归地克隆指定节点下的子树,则为true;否则false 仅克隆节点本身。

返回:

克隆的节点。

以下示例仅克隆没有子节点的节点本身。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode newBody = HtmlNode.CreateNode("<body></body>");newBody.CopyFrom(htmlBody, false);

public static HtmlNode CreateNode(string html)

从表示文字HTML的字符串创建一个HTML节点。 CreateNode方法是HtmlAgilityPack.HtmlNode 的成员

参数:

html: HTML文本。

返回:

新创建的节点实例。

以下示例创建一个HTML节点并添加为子节点。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode newPara = HtmlNode.CreateNode("<p>This a new paragraph</p>");htmlBody.ChildNodes.Add(newPara);

public HtmlNode InsertAfter(HtmlNode newChild, HtmlNode refChild)

在指定的参考节点之后立即插入指定的节点。InsertAfter方法是HtmlAgilityPack.HtmlNode 的成员

参数:

newChild: 要插入的节点。 不可能为null。

refChild: 作为参考节点的节点。 newNode放置在refNode之后。

返回:

正在插入的节点。

例子

以下示例创建一个HTML节点并在第一个孩子之后插入此节点

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode refChild = htmlBody.ChildNodes[1];HtmlNode newChild = HtmlNode.CreateNode("<p> This is inserted after node paragraph</p>");htmlBody.InsertAfter(newChild, refChild);

public HtmlNode InsertBefore(HtmlNode newChild, HtmlNode refChild)

在指定的参考节点之前立即插入指定的节点。 InsertBefore方法是HtmlAgilityPack.HtmlNode 的成员

参数:

newChild:要插入的节点。 不可能为null。

refChild: 作为参考节点的节点。 newChild放在这个节点之前。

返回:

正在插入的节点。

以下示例创建一个HTML节点并在第一个子节点之前插入此节点。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode refChild = htmlBody.ChildNodes[1];HtmlNode newChild = HtmlNode.CreateNode("<h1> This is inserted before node heading</h>");htmlBody.InsertBefore(newChild, refChild);

public HtmlNode PrependChild(HtmlNode newChild)

将指定的节点添加到此节点的子节点列表的开头。 PrependChild方法是HtmlAgilityPack.HtmlNode 的成员

参数:

newChild: 要添加的节点。 不可能为null。

返回:

添加的节点。

以下示例在开始处添加子节点。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode newChild = HtmlNode.CreateNode("<h1> This is added at the beginning</h>");htmlBody.PrependChild(newChild);

public void PrependChildren(HtmlNodeCollection newChildren)

将指定的节点列表添加到此节点的子节点列表的开头。 PrependChildren方法是HtmlAgilityPack.HtmlNode 的成员

参数:

newChildren:要添加的节点列表。不可能为null。

例子

以下示例将节点列表添加到此节点的子节点列表的开头。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode h1Node = HtmlNode.CreateNode("<h1> This is new heading</h1>");HtmlNode pNode = HtmlNode.CreateNode("<p> This is new paragraph 1</p>");HtmlNodeCollection newChildren = new HtmlNodeCollection(htmlBody);newChildren.Add(pNode);newChildren.Add(h1Node);htmlBody.PrependChildren(newChildren);

public void Remove()

从父集合中删除节点。 Remove方法是HtmlAgilityPack.HtmlNode 的成员

以下示例从父集合中删除第一个子节点,但该节点仍然存在。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode node = htmlBody.ChildNodes[1];node.Remove();

public void RemoveAll()

删除当前节点的所有子节点和/或属性。 RemoveAll方法是HtmlAgilityPack.HtmlNode 的成员

以下示例从父集合中删除第一个子节点及其所有子节点和属性。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode node = htmlBody.ChildNodes[1];node.RemoveAll();

public void RemoveAllChildren()

删除当前节点的所有子节点。 RemoveAllChildren方法是HtmlAgilityPack.HtmlNode 的成员

以下示例删除当前节点的所有子节点。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");htmlBody.RemoveAllChildren();

public HtmlNode RemoveChild(HtmlNode oldChild)

删除指定的子节点。 RemoveChild方法是HtmlAgilityPack.HtmlNode 的成员

参数:

oldChild: 被删除的节点。 不可能为null。

返回:

被删除的节点。

以下示例删除子节点h1。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode node = htmlBody.ChildNodes[1];htmlBody.RemoveChild(node);

public HtmlNode RemoveChild(HtmlNode oldChild, bool keepGrandChildren)

keepGrandChildren: true to keep grand children ofthe node, false otherwise.

Returns:

The node removed.

Examples

删除指定的子节点。 RemoveChild方法是HtmlAgilityPack.HtmlNode 的成员

参数:

oldChild: 被删除的节点。不可能为null。

keepGrandChildren:true保持节点的孙子节点,否则为false。

返回:

被删除的节点。

下面的示例删除子节点,但将保留孙子节点。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode node = htmlBody.ChildNodes[1];htmlBody.RemoveChild(node, true);

public HtmlNode ReplaceChild(HtmlNode newChild, HtmlNode oldChild)

newChild节点替换子节点oldChildReplaceChild方法是HtmlAgilityPack.HtmlNode 的成员

参数:

newChild: 放入子列表的新节点。

oldChild:列表中被替换的节点。

返回:

被替换的节点。

以下示例用newChild节点替换oldChild节点。

var htmlDoc = new HtmlDocument();htmlDoc.LoadHtml(html);var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");HtmlNode oldChild = htmlBody.ChildNodes[1];HtmlNode newChild = HtmlNode.CreateNode("<h2> This is h2 new child heading</h2>");htmlBody.ReplaceChild(newChild, oldChild);