Introduction C # two ways to read XML

Source: Internet
Author: User

XML Functions

For XML, I think everyone knows it well. I don't need to bother to describe what it is. I think XML will surely shine in the future of Web development, XML is a scalable markup language that allows enterprises to develop their own data formats. For Internet data transmission, I think this is the most attractive place for our programmers!

Our topic today is not about the advantages of XML, but about how to use XML in C. Next, let's take a look at some basic theoretical knowledge about accessing XML using a program.

Two access models:

There are two models for accessing and then operating XML files in a program: DOM (Document Object Model) and stream model. The advantage of DOM is that it allows editing and updating XML documents, you can randomly access the data in the document and use XPath for query. However, the disadvantage of DOM is that it needs to load the entire document to the memory at a time. For large documents, this may cause resource problems. The stream model solves this problem well because it uses the stream concept to access XML files, that is, it only has the current node in the memory at any time, but it also has its shortcomings. It is read-only and only forward, and cannot perform backward navigation in the document. Although each has its own merits, we can also use the two in the program to complement each other.

I. DOM Document Object Model Operations

Copy codeThe Code is as follows: using System. Xml;

XmlDocument xml = new XmlDocument (); // Initialize an xml instance
Xml. Load (path); // import the specified xml file
Xml. Load (HttpContext. Current. Server. MapPath ("~ /File/bookstore. xml "); XmlNode root = xml. SelectSingleNode ("/root "); // specify a node
XmlNodeList childlist = root. ChildNodes; // retrieve all direct subnodes under a node
XmlNodeList nodelist = xml. selectNodes ("/Root/News"); // obtain the set of nodes with the same name as the string id = node. attributes ["id"]. value; // obtain the specified attribute Value of a specified Node
String content = node. InnerText; // get the text in the specified node
Root. HasChildNodes; // determines whether a subnode exists under the node.

1. attributes of the XmlDocument class

The attributes and descriptions of the XmlDocument class are shown in the following table.

Attribute

Description

Attributes

Attribute Set of the current node

BaseURI

Base URI of the current node

ChildNodes

All subnodes of a node

DocumentElement

Document Root

DocumentType

Nodes declared by DOCTYPE

FirstChild

The first subnode of the node.

HasChildNodes

Is there any subnode?

Implementation

Get the XmlImplementation object of the current document

InnerText

All text content contained in the node

InnerXml

All XML content contained by the node

IsReadOnly

Whether the current node is read-only

Item

Obtains the specified child element.

LastChild

Last subnode

LocalName

Obtain the local name of a node

Name

Get the node's qualified name

NamespaceURI

Get the namespace URI of the node

NameTable

Obtain the XmlNameTable associated with this implementation

NextSibling

Obtain the node that follows the node

NodeType

Obtains the type of the current node.

OuterXml

Obtain the flag indicating this node and all its subnodes

OwnerDocument

Obtains the XmlDocument to which the current node belongs.

ParentNode

Obtain the parent level of a node (for a node with a parent level)

Prefix

Get or set the namespace prefix of the node

PreserveWhitespace

Gets or sets a value that indicates whether to retain white space in the element content.

Previussibling

Obtain the node that follows the node

SchemaInfo

Returns the post-architecture verification information set (PSVI) of the node)

Schemas

Gets or sets the XmlSchemaSet object associated with this XmlDocument

Value

Obtains or sets the value of a node.

XmlResolver

Set XmlResolver for parsing external resources

2. methods of the XmlDocument class

The methods and descriptions of the XmlDocument class are shown in the following table.

Method

Description

AppendChild

Add the specified node to the end of the node's subnode list

CreateAttribute

Create an XmlAttribute with the specified name

CreateCDataSection

Create an XmlCDataSection containing the specified data

CreateComment

Create an XmlComment that contains the specified data

CreateDocumentFragment

Create XmlDocumentFragment

CreateDocumentType

Returns the new XmlDocumentType object.

CreateElement

Create XmlElement

CreateEntityReference

Create an XmlEntityReference with the specified name

CreateNavigator

Create a new XPathNavigator object used to navigate this document

CreateNode

Create XmlNode

CreateProcessingInstruction

Create an XmlProcessingInstruction with the specified name and Data

CreateSignificantWhitespace

Create an XmlSignificantWhitespace Node

CreateTextNode

Create XmlText with specified text

CreateWhitespace

Create an XmlWhitespace Node

CreateXmlDeclaration

Create an XmlDeclaration node with the specified value

GetElementById

Obtains the XmlElement with the specified ID.

GetElementsByTagName

Returns an XmlNodeList that contains a list of all elements that match the specified name.

GetNamespaceOfPrefix

Find the xmlns declaration closest to the given prefix within the current node range, and return the namespace URI in the Declaration

GetPrefixOfNamespace

Find the xmlns declaration closest to the specified namespace URI within the current node range, and return the prefix defined in the Declaration

GetType

Obtains the Type of the current instance.

ImportNode

Import a node from another document to the current document

InsertAfter

Insert the specified node to the referenced node.

InsertBefore

Insert the specified node to the specified referenced node.

Load

Load specified XML data

LoadXml

Load the XML document from the specified string

Normalize

Convert all XmlText nodes to "normal" Form

PrependChild

Add a specified node to the beginning of the node's subnode list

ReadNode

Create an XmlNode object based on the information in XmlReader. The reader must be located on a node or attribute.

RemoveAll

Removes all subnodes and/or attributes of the current node.

RemoveChild

Removes a specified subnode.

ReplaceChild

Replace Old nodes with new nodes

Save

Save the XML document to the specified location

SelectNodes

Select the node list that matches the XPath expression

SelectSingleNode

Select the first XmlNode that matches the XPath expression

Supports

Test whether DOM implements specific functions

Validate

Verify whether the XmlDocument is an XML Schema Definition Language (XSD) architecture contained in the Schemas attribute.

WriteContentTo

Saves all child levels of the XmlDocument node to the specified XmlWriter.

WriteTo

Saves the XmlDocument node to the specified XmlWriter

The following describes common methods.

(1) Load Method

This method can be used to import XML data from an XML file specified by a string, a stream object, a TextReader object, and an XmlReader object.

(2) LoadXml Method

This method imports XML data from a specific XML file. By default, the LoadXml method neither retains blank nor meaningful blank. This method does not perform DTD or architecture verification.

(3) Save Method

This method saves XML data to an XML file or a stream object, a TextReader object, and an XmlReader object to import XML data.

3. Events of the XmlDocument class

The events and descriptions of the XmlDocument class are shown in the following table.

Table events and descriptions of the XmlDocument class

Event

Description

NodeChanged

This occurs when the Value of the node that belongs to this document has been changed.

NodeChanging

This occurs when the Value of the node that belongs to this document is changed.

NodeInserted

This occurs when the node that belongs to this document has been inserted into another node.

NodeInserting

This occurs when the node that belongs to this document is inserted into another node.

NodeRemoved

This occurs when the node that belongs to this document has been removed from its parent.

NodeRemoving

This occurs when the node that belongs to this document is removed from the document.

Http://kb.cnblogs.com/page/42226/

Related Article

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.