Three Modes for accessing XML data

Source: Internet
Author: User
Tags stream api
Tree-Based APIS
The tree model API presents the XML document as a tree composed of nodes, which can be immediately loaded into the memory. The most common XML tree model API is the W3C Document Object Model (DOM ). Dom supports reading, processing, and modifying XML documents programmatically.

The following example is obtained using the xmldocument class in. NET Framework.
Items
The first element
Compact-disc
Artist name and title.

Using system;
Using system. xml;

Public class test {

Public static void main (string [] ARGs ){

Xmldocument Doc = new xmldocument ();
Doc. Load ("test. xml ");

Xmlelement firstcd = (xmlelement) Doc. documentelement. firstchild;
Xmlelement artist =
(Xmlelement) firstcd. getelementsbytagname ("artist") [0];
Xmlelement Title =
(Xmlelement) firstcd. getelementsbytagname ("title") [0]

Console. writeline ("artist = {0}, Title = {1}", artist. innertext, title. innertext );
}
}

Cursor-based API
XML
The cursor API is like a lens that moves in an XML document, aiming at all aspects of the targeted document .. Xpathnavigator in Net Framework
Class is an XML cursor API. Compared with the tree model API, the XML cursor API has the advantage of not loading the entire document into the memory, which facilitates
GenerateProgramOptimize the part of the document generated as needed.

The following example is obtained using the xpathnavigator class in. NET Framework.
Items
The first element
Compact-disc
Artist name and title.

Using system;
Using system. xml;
Using system. xml. XPath;

Public class test {

Public static void main (string [] ARGs ){

Xmldocument Doc = new xmldocument ();
Doc. Load ("test. xml ");

Xpathnavigator nav = Doc. createnavigator ();

Nav. movetofirstchild (); // move from the root node to the document element (items)
Nav. movetofirstchild (); // move from the items element to the first compact-disc Element

// Move from the Compact-disc element to the artist Element
Nav. movetofirstchild ();
Nav. movetonext ();
String artist = nav. value;

// Move from the artist element to the title Element
Nav. movetonext ();
String title = nav. value;

Console. writeline ("artist = {0}, Title = {1}", artist, title );
}
}

Stream API
Enable
When processing XML Stream APIs, you only need to store the context of the current node to be processed in the memory to process XML documents. This type of API can process large XML
File, without occupying a large amount of content space. Streaming APIs for XML processing are mainly divided into two types: the push-based XML analyzer and the pull-based XML analyzer.

Base
Analyzer (such as sax) is used to move data in XML data streams and
The event is "Pushed" to the registered event handler (callback method ). The pulling-out-based analyzer (such as the xmlreader class in. NET Framework) is
The XML data stream is used as a forward-only cursor.

The following example is obtained using the xmlreader class in. NET Framework.
Items
The first element
Compact-disc
Artist name and title.

Using system;
Using system. xml;

Public class test {

Public static void main (string [] ARGs ){

String artist = NULL, Title = NULL;
Xmltextreader reader = new xmltextreader ("test. xml ");

Reader. movetocontent (); // move from root node to document element (items)

/* Keep reading until the first <artist> element is obtained */
While (reader. Read ()){

If (reader. nodetype = xmlnodetype. element) & reader. Name. Equals ("artist ")){

Artist = reader. readelementstring ();
Title = reader. readelementstring ();
Break;
}
}
Console. writeline ("artist = {0}, Title = {1}", artist, title );
}
}
Access XML data

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. xml;
Namespace consoleapplication1
{
Class Program
{
Static void main (string [] ARGs)
{
Xmldocument Doc = new xmldocument (); // create a Document Object
Try
{
Doc. Load ("../myorder. xml"); // load the XML Object
Xmlnode root = Doc. documentelement; // get the document root node
Xmlnode tempnode;
If (root. haschildnodes)
{
Tempnode = root. firstchild; // the first byte of the Root Node
}
Else
{
Tempnode = root;

}
While (tempnode! = Root)
{
Console. Write (tempnode. Name );
Tempnode = tempnode. firstchild; // get the node's byte
// Determine whether the node is a text node
If (tempnode. GetType (). Name = "xmltext ")
{
Console. writeline (":" + tempnode. value );
While (tempnode. nextsibling = NULL & tempnode! = Root)
{
Tempnode = tempnode. parentnode;
}
If (tempnode! = Root)
Tempnode = tempnode. nextsibling; // obtain the next node

}
Else
{
Console. writeline ();
}
}
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
Console. Readline (); // Auxiliary Code Used to retain the console window
}
}
}

From: http://www.cnblogs.com/xiangxiang/archive/2006/09/13/503499.html

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.