Here we will describe how to use the object model of the xml dom document. The object model of the DOM document allows you to read, process, and modify the XML document programmatically. For details, see the following.
Xml dom Document Object Model
The xml dom Document Object Model class is the memory representation of the XML document. The DOM Document Object Model allows you to read, process, and modify XML documents programmatically. The XmlReader class also reads XML, but it provides non-cached inbound and read-only access. This means that XmlReader cannot edit attribute values or element content, nor insert or remove nodes. Editing is the main function of DOM Document Object Model. XML data representation in memory is a common structured method, although the actual XML data is stored in a linear manner when it is in a file or when it is passed in from another object. The following are XML data.
Input
- <?xmlversionxmlversion="1.0"?>
- <books>
- <book>
- <author>Carson</author>
- <priceformatpriceformat="dollar">31.95</price>
- <pubdate>05/01/2001</pubdate>
- </book>
- <pubinfo>
- <publisher>MSPress</publisher>
- <state>WA</state>
- </pubinfo>
- </books>
Displays how the memory is constructed when the XML data is read into the DOM Document Object Model structure.
XML document structure
In the XML document structure, each circle in this figure represents a node called an XmlNode object ). The XmlNode object is the basic object in the DOM tree. XmlDocument class extension XmlNode) supports the method used to perform operations on the entire document, such as loading the document into memory or saving the XML to the file. In addition, XmlDocument provides a way to view and process nodes in the entire XML document. Both XmlNode and XmlDocument have enhanced performance and availability, and perform the following operations through methods and attributes:
◆ Access and modify specific nodes of the DOM Document Object Model, such as element nodes and Object Reference nodes.
◆ In addition to retrieving information contained in a node, such as text in an element node, the entire node is also retrieved.
Note:
If the application does not require the structure or editing function provided by DOM, The XmlReader and XmlWriter classes provide non-cached inbound access to XML. For more information, see read XML using XmlReader and write XML using XmlWriter.
Node objects have a set of methods and attributes, as well as basic and well-defined features. Some of these features include:
◆ A node has a single parent node. The parent node is a top-level node adjacent to the node. The only node without a parent level is the document root because it is a top-level node that contains the document itself and the document fragment.
◆ Most nodes can have multiple subnodes, which are adjacent to the next level of the node. Below is a list of node types that can have subnodes.
Document
DocumentFragment
EntityReference
Element
Attribute
XmlDeclaration, Notation, Entity, CDATASection, Text, Comment, ProcessingInstruction, and DocumentType nodes have no subnodes.
◆ In the same level, the nodes represented by the book and pubinfo nodes in the graph are peers.
One feature of the DOM Document Object Model is to process attributes. An attribute is a node that does not belong to a parent-child relationship or a peer relationship. An attribute is considered as an attribute of an element node. It consists of a name and a value pair. For example, if XML data is composed of format = "dollar" associated with the element price, the Word format is the name, and the value of the format attribute is dollar. To retrieve the format = "dollar" attribute of a price node, you can call the GetAttribute method when the cursor is located at the price element node. For more information, see accessing attributes in the DOM.
When you read XML into the memory, a node is created. However, not all nodes are of the same type. Elements in XML have different rules and syntaxes for processing commands. Therefore, when reading various data, a node type is assigned to each node. This node type determines the features and functions of the node.
For more information about node types generated in memory, see XML node types. For more information about the objects created in the node tree, see ing object hierarchies to XML data.
Microsoft has extended the APIs available in the W3C DOM level 1 and level 2, making it easier to use XML documents. While fully supporting W3C standards ,?? Added classes, methods, and attributes that cannot be completed using W3CXMLDOM. The new class allows you to access relational data, and provides you with the method of synchronizing data with ADO. NET and making data public as XML. For more information, see synchronize DataSet with XmlDataDocument.
The DOM Document Object Model is most useful when you read XML data into the memory to change its structure, add or remove nodes, or modify the data stored by nodes as in the text contained in the element. However, in other schemes, there are other classes that are faster than DOM. To enable quick non-Cache access to XML, use XmlReader and XmlWriter. If you need to use the cursor model and XPath for random access, use the XPathNavigator class.