Basic application examples quickly enter the world of XML programming

Source: Internet
Author: User
Tags object net object model reference string access
Xml| Programming | Application Examples This article introduces 3 basic examples of XML that are designed to lead you quickly into the world of XML programming. Examples include: in. NET using XML, reading XML files, inserting data into XML documents.

  using XML in. NET
If you have used MSXML3, then in. NET application will be a fairly straightforward process. Instant no contact with MSXML3, nor does it matter that you will find use. NET platform is also a very easy thing to do with the related classes.

There are two main APIs that can be used to access data that is built into XML documents, including forward-only, buffer-free access and random access, and are used throughout the Document Object model DOM. Classes about these 2 APIs are located in the System.Xml collection.

If you want to quickly and efficiently access data in an XML document, you need to use the XmlTextReader class. This class is handled in a "pull" mode, which is much superior to the "push" mode in the simple XML API (SAX). Before using the XmlTextReader class, you first reference the System.Xml collection, which is referenced in C # using the "using" keyword, and the "imports" keyword in Visual Basic. After you have referenced the collection, you can begin the example read operation as shown in the following code:

XmlTextReader reader = new XmlTextReader (Pathtoxmldoc);
int elementcount = 0;
while (reader. Read ()) {
if (reader. NodeType = = XmlNodeType.Element) {
elementcount++;
}
}


There are several different constructors in the XmlTextReader class, as shown above to receive a path for an XML file as a string parameter.

Although forward-only pull mode processing is fairly efficient, it is read-only and cannot be allowed to perform inserts, deletes, or updates to an XML document node. When you need to exert more control over the XML document and need more flexibility, we can look at the Document Object model DOM. The function of the DOM API loads each node in the XML document into a tree structure, which looks like a "family tree". With this structure in memory, it becomes feasible to randomly access different nodes in an XML document.

Before you start creating a DOM tree structure, you first reference the System.Xml collection, and then the XmlDocument class:

XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (Pathtoxmldoc);
XmlNode root = xmldoc.documentelement;


By using the relevant methods in the XmlDocument class, adding nodes to a tree structure can be done easily. The following example shows how to load XML from a file, and then add a child element and its associated properties under 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 above code executes, 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. Once loaded, you can manipulate the XML in the following manner:

String myxml = "<root><someNode>Hello</someNode></root>";
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.loadxml (myXML);
... manipulation or reading of the nodes can occur here


In addition to the above, there are several other classes in the System.Xml collection that can be used to perform different tasks. The above description is only a taste, and a lot of applications need more practice.

[1] [2] [3] Next page



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.