c# IHTMLDomNode Vs IHTMLElement

来源:互联网 发布:java 泛型变量 编辑:程序博客网 时间:2024/06/05 20:32

首先根据DOM(document object model)协议, 对于一个html文件 可以把它按照各个标签相互包含而确定父子节点的方式 以<body> 为root 建立一颗树。 这里并非一个节点对应一个标签, 具体请参照w3c标准 http://www.w3school.com.cn/htmldom/index.asp


有了这个树的概念, 那么ihtmlDomNode 和 ihtmlElement是c# 提供的两个接口以查询这颗树中节点的信息。他们侧重点各有不同, 以下是msdn的解释:

IHTMLDOMNode interface: Provides methods to access all of the nodes in the document object model (DOM), to iterate the nodes, to insert nodes, to remove nodes, and to get the attributes of a node.

IHTMLElement interface : This interface provides the ability to programmatically access the properties and methods that are common to all element objects.

我个人的理解是这样, ihtmlDomNode 更侧重的是这个节点对于这颗树而言的一些信息,比如遍历他的子节点,父节点,插入删除相关节点等等。 而ihtmlElement我认为是更侧重于该节点的属性信息,如这个节点对应html标签的一些属性设置等等, 具体看他们各自提供的方法即可明白。


这里说下ihtmlDomNode的nodevalue属性 和 ihtmlElement的innertext属性的区别 , 这也是前面我比较纠结的地方:

nodeValue: gets or sets the value of a node.

innertext:   sets or retrieves the text between the start and end tags of the  object

这里nodeValue的话又得分node的种类,比较常见的三种node类型,文本节点/属性节点/元素节点, 他们返回的nodevalue各有不同, 分别是 文本, 属性和null。

innertext的话解释的比较清楚, 是一个标签里所有子标签 text的和。 注意必须得有start&end tag的才行, 否则返回null。 同时对于块级元素的innertext是read only的,而内联元素可以设置。