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.

輸入Input

<?xml version="1.0"?>
  <books>
    <book>
        <author>Carson</author>
        <price format="dollar">31.95</price>
        <pubdate>05/01/2001</pubdate>
    </book>
    <pubinfo>
        <publisher>MSPress</publisher>
        <state>WA</state>
    </pubinfo>
  </books>

下圖顯示當這個 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. 擴充 XmlNodeXmlDocument 類別支援在文件上整體執行作業的方法,例如,將它載入記憶體或將 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. XmlNodeXmlDocument 都可加強效能和可用性,而且有方法和屬性可以: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 所提供的結構或編輯功能,則 XmlReaderXmlWriter 類別會提供對 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

    XmlDeclarationNotationEntityCDATASectionTextCommentProcessingInstructionDocumentType 節點都沒有子節點。The XmlDeclaration, Notation, Entity, CDATASection, Text, Comment, ProcessingInstruction, and DocumentType nodes do not have child nodes.

  • 相同層級的節點,在圖表中以 bookpubinfo 節點呈現,是同層級 (Sibling)。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. 屬性 (Attribute) 會被視為項目節點的屬性 (Property),由一個名稱和一個值配對組成。Attributes are considered a property of the element node and are made up of a name and a value pair. 例如,如果 XML 資料是由與項目 format="dollar 關聯的 price" 所組成,則 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 已擴充 API,使其可在全球資訊網協會 (W3C) DOM 層級 1 和層級 2 中使用,讓您得以更輕鬆地使用 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.

DOM 對於將 XML 資料讀入記憶體以變更它的結構、加入或移除節點,或修改在項目所包含之內容中的節點所儲存的資料時最有用。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 存取,請使用 XmlReaderXmlWriterFor 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