XML 文档对象模型 (DOM)XML Document Object Model (DOM)

XML 文档对象模型 (DOM) 类是 XML 文档的内存中表示形式。The XML Document Object Model (DOM) class is an in-memory representation of an XML document. DOM 使您能够以编程方式读取、处理和修改 XML 文档。The DOM allows you to programmatically read, manipulate, and modify an XML document. 虽然 XmlReader 类也读取 XML,但它提供的是非缓存、仅正向的只读访问。The XmlReader class also reads XML; however, it provides non-cached, forward-only, read-only access. 也就是说,使用 XmlReader 无法编辑属性值或元素内容,也无法插入和删除节点。This means that there are no capabilities to edit the values of an attribute or content of an element, or the ability to insert and remove nodes with the XmlReader. 编辑是 DOM 的主要功能。Editing is the primary function of the DOM. XML 数据在内存中表示是常见的结构化方法,尽管实际的 XML 数据在文件中时或从另一个对象传入时以线性方式存储。It is the common and structured way that XML data is represented in memory, although the actual XML data is stored in a linear fashion when in a file or coming in from another object. 以下是 XML 数据。The following is XML data.


<?xml version="1.0"?>
        <price format="dollar">31.95</price>

下图显示将此 XML 数据读入 DOM 结构中时如何构造内存。The following illustration shows how memory is structured when this XML data is read into the DOM structure.

XML 文档结构XML 文档结构XML document structure XML document structure

在 XML 文档结构中,此图中的每个圆圈表示一个节点(称为 XmlNode 对象)。Within the XML document structure, each circle in this illustration represents a node, which is called an XmlNode object. XmlNode 对象是 DOM 树中的基本对象。The XmlNode object is the basic object in the DOM tree. XmlDocument 类(扩展 XmlNode)支持用于对整个文档执行操作(例如,将文档加载到内存中或将 XML 保存到文件中)的方法。The XmlDocument class, which extends XmlNode, supports methods for performing operations on the document as a whole (for example, loading it into memory or saving the XML to a file. 此外,XmlDocument 还支持查看和控制整个 XML 文档中的节点。In addition, XmlDocument provides a means to view and manipulate the nodes in the entire XML document. XmlNode 和 XmlDocument 都增强了性能和可用性,并包含执行下列操作的方法和属性:Both XmlNode and XmlDocument have performance and usability enhancements and have methods and properties to:

  • 访问和修改 DOM 特定的节点,如元素节点、实体引用节点等。Access and modify nodes specific to the DOM, such as element nodes, entity reference nodes, and so on.

  • 除检索节点包含的信息(如元素节点中的文本)外,还检索整个节点。Retrieve entire nodes, in addition to the information the node contains, such as the text in an element node.


    如果应用不需要 DOM 提供的结构或编辑功能,XmlReader 和 XmlWriter 类提供对 XML 的非缓存、仅正向流访问。If an application does not require the structure or editing capabilities provided by the DOM, the XmlReader and XmlWriter classes provide non-cached, forward-only stream access to XML. 有关详细信息,请参阅 XmlReaderXmlWriterFor more information, see XmlReader and XmlWriter.

Node 对象包含一组方法和属性,以及定义完善的基本特性。Node objects have a set of methods and properties, as well as basic and well-defined characteristics. 其中的某些特性包括:Some of these characteristics are:

  • 节点有单个父节点,父节点是与节点相邻的上一级节点。Nodes have a single parent node, a parent node being a node directly above them. 唯一没有父级的节点是文档根,因为它是顶级节点,包含了文档本身和文档片段。The only nodes that do not have a parent is the Document root, as it is the top-level node and contains the document itself and document fragments.

  • 大多数节点可以有多个子节点,子节点是与节点相邻的下一级节点。Most nodes can have multiple child nodes, which are nodes directly below them. 以下是可以有子节点的节点类型列表。The following is a list of node types that can have child nodes.

    • DocumentDocument

    • DocumentFragmentDocumentFragment

    • EntityReferenceEntityReference

    • 元素Element

    • 特性Attribute

    XmlDeclaration、Notation、Entity、CDATASection、Text、Comment、ProcessingInstruction 和 DocumentType 节点没有子节点。The XmlDeclaration, Notation, Entity, CDATASection, Text, Comment, ProcessingInstruction, and DocumentType nodes do not have child nodes.

  • 关系图中由 book 和 pubinfo 节点表示的同一级别节点是同级。Nodes that are at the same level, represented in the diagram by the book and pubinfo nodes, are siblings.

DOM 的一个特性是处理属性的方式。One characteristic of the DOM is how it handles attributes. 属性是不属于父子关系和同级关系的节点。Attributes are not nodes that are part of the parent, child, and sibling relationships. 属性被视为元素节点的属性,由名称和值对组成。Attributes are considered a property of the element node and are made up of a name and a value pair. 例如,如果存在由与元素 format="dollar 关联的 price" 组成的 XML 数据,则单词 format 是名称,format 属性的值是 dollarFor example, if you have XML data consisting of format="dollar" associated with the element price, the word format is the name, and the value of the format attribute is dollar. 若要检索 price 节点的 format="dollar" 属性,请在游标位于 price 元素节点时,调用 GetAttribute 方法。To retrieve the format="dollar" attribute of the price node, you call the GetAttribute method when the cursor is located at the price element node. 有关详细信息,请参阅访问 DOM 中的属性For more information, see Accessing Attributes in the DOM.

将 XML 读入内存时会创建节点。As XML is read into memory, nodes are created. 然而,并非所有节点都是同一类型。However, not all nodes are the same type. XML 中的元素具有不同于处理指令的规则和语法。An element in XML has different rules and syntax than a processing instruction. 因此,在读取各种数据时,将为每个节点分配一种节点类型。Therefore, as various data is read, a node type is assigned to each node. 此节点类型确定节点的特性和功能。This node type determines the characteristics and functionality of the node.

若要详细了解在内存中生成的节点类型,请参阅 XML 节点类型For more information on the types of nodes generated in memory, see Types of XML Nodes. 若要详细了解在节点树中创建的对象,请参阅将对象层次结构映射到 XML 数据For more information on the objects created in the node tree, see Mapping the Object Hierarchy to XML Data.

Microsoft 扩展了万维网联合会 (W3C) DOM 级别 1 和级别 2 中可用的 API,使 XML 文档的使用更容易。Microsoft has extended the APIs that are available in the World Wide Web Consortium (W3C) DOM Level 1 and Level 2 to make it easier to work with an XML document. 在完全支持 W3C 标准的同时,附加的类、方法和属性增加了使用 W3C XML DOM 无法完成的功能。While fully supporting the W3C standards, the additional classes, methods, and properties add functionality beyond what can be done using the W3C XML DOM. 新类使您能够访问关系数据,为您提供与 ADO.NET 数据同步、同时将数据作为 XML 公开的方法。New classes enable you to access relational data, giving you methods for synchronizing with ADO.NET data, simultaneously exposing data as XML. 有关详细信息,请参阅将 DataSet 与 XmlDataDocument 同步For more information, see Synchronizing a DataSet with an XmlDataDocument.

在将 XML 数据读入内存,以更改其结构、添加或移除节点或者与在元素包含的文本中一样修改节点所保存的数据时,DOM 最有用。The DOM is most useful for reading XML data into memory to change its structure, to add or remove nodes, or to modify the data held by a node as in the text contained by an element. 不过,在其他方案中,还有其他比 DOM 更快的类。However, other classes are available that are faster than the DOM in other scenarios. 若要对 XML 执行非缓存、仅正向的快速流访问,请使用 XmlReader 和 XmlWriter。For fast, non-cached, forward-only stream access to XML, use the XmlReader and XmlWriter. 如果需要使用游标模型和 XPath 进行随机访问,请使用 XPathNavigator 类。If you need random access with a cursor model and XPath, use the XPathNavigator class.

另请参阅See also