The DOM model of C # XML parsing

Source: Internet
Author: User

The DOM works by first loading the XML document into memory one at a time, and then creating a "tree structure" in memory based on the elements and attributes defined in the document, which is a Document object model, meaning that the document is object-oriented, and each node in the document corresponds to an object in the model. And we all know that objects provide programming interfaces, so in application we use this set of objects to access XML documents and manipulate XML documents.

Illustrates the process of application and DOM interaction:

Dom creates a tree model in memory:

Fragment of XML document:

<parent>

<child id= "123" >text here</child>

</parent>

The tree structure in memory is as follows:

The main steps of C # to handle the DOM:

1. Declare the XmlDocument object and load the XML file or fragment;

Xmldocument doc=new Xmldocument (); Don't forget to import System.Xml

Doc.load ("Test.xml");//Load XML file

Doc.loadxml ("<book>abc</book>"); reprint XML Fragments

2. Declare the XmlElement variable object and assign it with doc.documentelement (the root element of the resulting XML file is not the root node)

XmlElement Root=doc. DocumentElement ();

3. Find the node and do the appropriate operation:

1) Find a single node: One method: Using the Get series method, such as Getfirstchild (), and the other method is selectSingleNode ("corresponding XPath string");

2) The lookup node set has two objects XmlNodeList and XmlNamedNodeMap, the former used more, the latter used to process the unordered node set processing XML fragments.

Method One

XmlNodeList El=n.childnodes; Get all child node sets of n

Method Two

XmlNodeList el=n.selectnodes ("book"); Gets the node set named book for the node

Method Three

XmlElement Rootele=doc. DocumentElement;

XmlNodeList el=rootele.getelementsbytagname ("book");

Method Four

XmlNodeList El=doc. getElementsByTagName ("book");

4. Create a new node main steps:

1) Use the XmlDocument object and populate it with XML

2) determine where to insert and navigate to this location

3) Create node (can also be: Element, Attribute, Text, Comment, CDATA, or processing &instruction)

4) Add the node to the Xmldocment object again

(Note: There are three ways to add a node.) AppendChild (), InsertBefore () and InsertAfter ();)

(i): Create ELEMENT nodes

XmlElement Elem=doc. CreateElement ("Price");

XmlElement (object). InnerText ();

XmlNode (object). createTextNode ();

(ii): setting Properties for ELEMENT nodes

SetAttribute () This method is a change, the non-invasive.

(iii): Create other types of nodes

Xmldocument.createcomment () method to annotate nodes

Xmldocument.createcdatasection ()

Xmldocument.createprocessinginstruction ()

5. Modifying and deleting nodes

ReplaceChild ();

SetAttribute ();

RemoveChild ();

RemoveAttribute ();

RemoveAll ();

6. Save the XML document

Xmldocument.save (XMLfileName);

Two important classes: XmlNode Class and XmlDocument class

The DOM model of C # XML parsing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.