Introduction to C # Two ways to read XML _c# tutorial

Source: Internet
Author: User
Tags xpath

XML role

For XML, I guess you all know better, I do not have to use the ink to describe what it is, I think in the future of web development in the XML will certainly shine, XML is Extensible Markup Language, the enterprise can develop a set of its own data format. Data transfer for the Internet, I think, is the most tempting part of XML for US programmers!

Our topic today is not about the benefits of XML, but about how to use XML in C #. Let's take a look at some of the basics of using programs to access XML.

Two models for access:

Accessing and then manipulating XML files in programs there are generally two models, using the DOM (Document Object model) and the flow model, the advantage of using the DOM is that it allows editing and updating of XML documents, random access to the data in the document, and the use of XPath queries, but The disadvantage of DOM is that it requires a one-time load of the entire document into memory, which can cause resource problems for large documents. The flow model solves the problem very well, because its access to the XML file is based on the concept of a stream, that is, there is only the current node in memory at any time, but it also has its drawbacks, it is read-only, forward only, and cannot perform backward navigation in the document. Although there are advantages and disadvantages, but we can also in the process of the two and the implementation of complementary, hehe

DOM Document Object Model operations

Copy Code code 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;//gets all the direct child nodes under the node
XmlNodeList Nodelist=xml. SelectNodes ("/root/news");//Gets the same sibling node collection string Id=node with the same name. attributes["id"]. value;//gets the specified attribute value for the specified node
String Content=node. innertext;//gets the text in the specified node
Root. haschildnodes;//determine if there are child nodes under this node

1. Properties of the XmlDocument class

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

Property

Description

Attributes

Collection of properties for the current node

BaseURI

Base URI of the current node

ChildNodes

All child nodes of a node

DocumentElement

The root of the document

DocumentType

Nodes declared by DOCTYPE

FirstChild

The first child node of a node

HasChildNodes

Whether there are any child nodes

Implementation

Gets the XmlImplementation object for the current document

InnerText

All text content that the node contains

InnerXml

All the XML content that the node contains

IsReadOnly

Whether the current node is read-only

Item

Gets the specified child element

LastChild

Last child node

LocalName

Gets the local name of the node

Name

Gets the qualified name of the node

NamespaceURI

Gets the namespace URI of the node

NameTable

Gets the xmlnametable associated with this implementation

NextSibling

Gets the node immediately after the node

NodeType

Gets the type of the current node

OuterXml

Gets a token representing this node and all its child nodes

Ownerdocument

Gets the XmlDocument that the current node belongs to

ParentNode

Gets the parent of this node (for nodes that can have a parent)

Prefix

Gets or sets the namespace prefix for this node

PreserveWhitespace

Gets or sets a value that indicates whether whitespace is preserved in element content

PreviousSibling

Gets the node immediately preceding the node

SchemaInfo

Returns the post-schema validation information set for a node (PSVI)

Schemas

Gets or sets the XmlSchemaSet object associated with this XmlDocument

Value

Gets or sets the value of a node

XmlResolver

Set up XmlResolver for resolving external resources

2. Methods of XmlDocument classes

The XmlDocument class method and description are shown in the following table.

Method

Description

AppendChild

Adds the specified node to the end of the list of child nodes of the node

CreateAttribute

Create a XmlAttribute with the specified name

Createcdatasection

Create a xmlcdatasection that contains the specified data

Createcomment

Create a xmlcomment that contains the specified data

Createdocumentfragment

Create XmlDocumentFragment

CreateDocumentType

Returns the new XmlDocumentType object

CreateElement

Create XmlElement

Createentityreference

Create a xmlentityreference with the specified name

CreateNavigator

Create a new XPathNavigator object for navigating this document

CreateNode

Create XmlNode

Createprocessinginstruction

Creates a xmlprocessinginstruction with the specified name and data

Createsignificantwhitespace

Create a XmlSignificantWhitespace node

createTextNode

Creates a XmlText with the specified text

Createwhitespace

Create a XmlWhitespace node

Createxmldeclaration

Creates a XmlDeclaration node with the specified value

getElementById

Gets the XmlElement with the specified ID

getElementsByTagName

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

Getnamespaceofprefix

Finds the closest xmlns declaration for the given prefix within the current node range and returns the namespace URI in the declaration

Getprefixofnamespace

Finds the closest xmlns declaration for the given namespace URI within the current node range and returns the prefix defined in the Declaration

GetType

Gets the Type of the current instance

ImportNode

Import a node from another document into the current document

InsertAfter

Inserts the specified node immediately after the specified reference node

InsertBefore

Inserts the specified node immediately before the specified reference node

Load

Loading the specified XML data

Loadxml

Loads an XML document from a specified string

Normalize

Convert the XmlText node to the "normal" form

PrependChild

Adds the specified node to the beginning of the list of child nodes of the node

ReadNode

Creates a XmlNode object based on the information in the XmlReader. The reader must be positioned on a node or property

RemoveAll

Remove all child nodes and/or attributes of the current node

RemoveChild

Removes the specified child node

ReplaceChild

Replace old node with new node

Save

To save an XML document to a specified location

SelectNodes

Select a list of nodes that match an XPath expression

selectSingleNode

Select the first XmlNode to match an XPath expression

Supports

Testing whether the DOM implementation implements specific functionality

Validate

Verify that XmlDocument is not an XML schema definition language (XSD) schema that is contained in the Schemas property.

WriteContentTo

Saves all children of the XmlDocument node to the specified XmlWriter

WriteTo

Saves the XmlDocument node to the specified XmlWriter

The following is an introduction to common methods.

(1) Load method

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

(2) Loadxml method

This method completes the ability to import XML data from a particular XML file. By default, the Loadxml method neither preserves whitespace nor preserves meaningful whitespace. This method does not perform DTD or schema validation.

(3) Save method

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

3. Events for XmlDocument classes

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

Table XmlDocument event and description of class

Event

Description

NodeChanged

Occurs when the value of the node that belongs to the document has been changed

Nodechanging

Occurs when the Value of the node that belongs to the document is changed

NodeInserted

Occurs when a node belonging to this document has been inserted into another node

Nodeinserting

Occurs when the node that belongs to the document is inserted into another node

Noderemoved

Occurs when a node belonging to the document has been removed from its parent

Noderemoving

Occurs when the node that belongs to the document is removed from the document

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

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.