Xmltextreader and xmldocument

Source: Internet
Author: User
This article introduces three basic XML application instances to help you quickly enter the XML programming world. Examples include: using XML in. net, reading XML files, and inserting data into XML documents.
Use XML in. net

If msxml3 is used, using XML in. NET applications is quite simple. It doesn't matter if you have never touched msxml3 in real time. You will find it easy to use the classes provided by the. NET platform.

There are two main APIs that can be used to access data created in XML documents, including forward-only unbuffered access and random access, and the Document Object Model Dom is used from beginning to end. The classes for these two APIs are located in the system. xml collection.

To quickly and effectively access data in XML documents, you need to use the xmltextreader class. This class adopts the "pull" mode, which is much better than the "push" Mode in simple xml api (SAX. Before using the xmltextreader class, you must first reference the system. xml set. in C #, the "using" keyword is used for reference. in Visual Basic, the "Imports" keyword is used. After the collection is referenced, you can start the reading operation as shown in the following code:

Xmltextreader reader = new xmltextreader (pathtoxmldoc );
Int elementcount = 0;
While (reader. Read ()){
If (reader. nodetype = xmlnodetype. element ){
Elementcount ++;
}
}

The xmltextreader class has several different constructors. the one shown above is responsible for receiving the path of an XML file as a string parameter.

Although only the forward PULL mode is efficient, it is read-only, so you cannot insert, delete, or update XML document nodes. When you need more control over XML documents and require more flexibility, you can take a look at the Document Object Model Dom. The dom api function loads every node in the XML document into a tree structure, which looks like a "genealogy ". With this structure in the memory, it is feasible to randomly access different nodes in the XML document.

Before creating a DOM tree structure, first reference the system. xml set and then demonstrate the xmldocument class:

Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (pathtoxmldoc );
Xmlnode root = xmldoc. documentelement;

By using the relevant methods in the xmldocument class, you can easily add nodes to the tree structure. The following example demonstrates how to load XML from a file and add a child element and its related attributes under the root node root:

Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (pathtoxmldoc );
Xmlelement root = xmldoc. documentelement;
Xmlelement newnode = Doc. createelement ("newnode ");
Newnode. setattribute ("ID", "1 ");
Root. appendchild (newnode );

After the preceding code is executed, the following XML document is generated:

<? XML version = "1.0"?>
<Root>
<Newnode id = "1"/>
</Root>

When you need to load a string containing XML into the Dom, you can use the loadxml () method of the xmldocument class. After loading the file, you can operate the XML as follows:

String myxml = "<root> <somenode> Hello </somenode> </root> ";
Xmldocument xmldoc = new xmldocument ();
Xmldoc. loadxml (myxml );
//... Manipulation or reading of the nodes can occur here

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.