C# XML parsing

Introduction to two ways of reading XML in C#

Author: Font: [ Increase and   decrease ] Type: Reprint time: 2013-03-03
There are generally two models for accessing and then manipulating XML files in a program, namely, the use of DOM (Document Object Model) and the stream model. The advantage of using DOM is that it allows editing and updating of XML documents, and random access to data in documents. You can use XPath query
 

XML role

    As for XML, I think everyone knows it well, so I don’t need to describe what it is. I think XML will shine in the future of Web development. XML is an extensible markup language. Using it, companies can formulate a Set your own data format. Data transfer for the Internet, I think, is the most enticing thing about XML for us programmers!

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

Two models of access:

    There are generally two models for accessing and then manipulating XML files in a program, namely, the use of DOM (Document Object Model) and the stream model. The advantage of using DOM is that it allows editing and updating of XML documents, and random access to data in documents. You can use XPath queries, however, have the disadvantage of DOM in that it needs to load the entire document into memory at once, which can cause resource problems for large documents. The stream model solves this problem very well, because its access to the XML file adopts the concept of stream, that is to say, there is only the current node in memory at any time, but it also has its shortcomings, it is read-only, Forward only, cannot perform backward navigation in the document. Although each has its own merits, we can also use both in the program to complement each other, huh, huh

1. DOM document object model operation

Copy the code The  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;//Get all direct child nodes under the node
XmlNodeList nodelist=xml.SelectNodes("/Root/News"); //Get the set of peer nodes with the same name string id=node.Attributes["id"].Value;//Get the specified attribute value of the specified node
string content=node.InnerText;//Get the text
root.HasChildNodes in the specified node; / / Determine whether there are child nodes under the node

1. Properties of the XmlDocument class

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

Attributes

illustrate

Attributes

The property collection of the current node

BaseURI

the base URI of the current node

ChildNodes

all children of the node

DocumentElement

the root of the document

DocumentType

Node declared by DOCTYPE

FirstChild

the first child of the node

HasChildNodes

have any child nodes

Implementation

Get the XmlImplementation object of the current document

InnerText

all text content contained in the node

InnerXml

All XML content contained in the node

IsReadOnly

Whether the current node is read-only

Item

Get the specified child element

LastChild

最后一个子节点

LocalName

获取节点的本地名称

Name

获取节点的限定名

NamespaceURI

获取该节点的命名空间 URI

NameTable

获取与此实现关联的 XmlNameTable

NextSibling

获取紧接在该节点之后的节点

NodeType

获取当前节点的类型

OuterXml

获取表示此节点及其所有子节点的标记

OwnerDocument

获取当前节点所属的 XmlDocument

ParentNode

获取该节点(对于可以具有父级的节点)的父级

Prefix

获取或设置该节点的命名空间前缀

PreserveWhitespace

获取或设置一个值,该值指示是否在元素内容中保留空白

PreviousSibling

获取紧接在该节点之前的节点

SchemaInfo

返回节点的后架构验证信息集 (PSVI)

Schemas

获取或设置与此 XmlDocument 关联的 XmlSchemaSet 对象

Value

获取或设置节点的值

XmlResolver

设置 XmlResolver 以用于解析外部资源

  2.XmlDocument类的方法

   XmlDocument类的方法及说明如下表所示。

方法

说明

AppendChild

将指定的节点添加到该节点的子节点列表的末尾

CreateAttribute

创建具有指定名称的 XmlAttribute

CreateCDataSection

创建包含指定数据的 XmlCDataSection

CreateComment

创建包含指定数据的 XmlComment

CreateDocumentFragment

创建 XmlDocumentFragment

CreateDocumentType

返回新的 XmlDocumentType 对象

CreateElement

创建 XmlElement

CreateEntityReference

创建具有指定名称的 XmlEntityReference

CreateNavigator

创建一个用于导航此文档的新 XPathNavigator 对象

CreateNode

创建 XmlNode

CreateProcessingInstruction

创建一个具有指定名称和数据的 XmlProcessingInstruction

CreateSignificantWhitespace

创建一个 XmlSignificantWhitespace 节点

CreateTextNode

创建具有指定文本的 XmlText

CreateWhitespace

创建一个 XmlWhitespace 节点

CreateXmlDeclaration

创建一个具有指定值的 XmlDeclaration 节点

GetElementById

获取具有指定 ID 的 XmlElement

GetElementsByTagName

返回一个 XmlNodeList ,它包含与指定名称匹配的所有元素的列表

GetNamespaceOfPrefix 

查找当前节点范围内离给定的前缀最近的 xmlns 声明,并返回声明中的命名空间 URI

GetPrefixOfNamespace 

查找当前节点范围内离给定的命名空间 URI 最近的 xmlns 声明,并返回声明中定义的前缀

GetType

获取当前实例的 Type

ImportNode

将节点从另一个文档导入到当前文档

InsertAfter

将指定的节点紧接着插入指定的引用节点之后

InsertBefore

将指定的节点紧接着插入指定的引用节点之前

Load

加载指定的 XML 数据

LoadXml

从指定的字符串加载 XML 文档

Normalize 

将XmlText 节点都转换成“正常”形式

PrependChild 

将指定的节点添加到该节点的子节点列表的开头

ReadNode

根据 XmlReader 中的信息创建一个 XmlNode 对象。读取器必须定位在节点或属性上

RemoveAll 

移除当前节点的所有子节点和/或属性

RemoveChild 

移除指定的子节点

ReplaceChild 

用新节点替换旧节点

Save

将 XML 文档保存到指定的位置

SelectNodes 

选择匹配XPath 表达式的节点列表

SelectSingleNode 

选择匹配 XPath 表达式的第一个 XmlNode

Supports 

测试 DOM 实现是否实现特定的功能

Validate

验证 XmlDocument 是不是 Schemas 属性中包含的 XML 架构定义语言 (XSD) 架构。

WriteContentTo

将 XmlDocument 节点的所有子级保存到指定的 XmlWriter 中

WriteTo

将 XmlDocument 节点保存到指定的 XmlWriter

  下面对常用的方法进行介绍。

  (1)Load方法

  该方法可以从一个字符串指定的XML文件或是一个流对象、一个TextReader对象、一个XmlReader对象导入XML数据。

  (2)LoadXml方法

  该方法完成从一个特定的XML文件导入XML数据的功能。默认情况下,LoadXml 方法既不保留空白,也不保留有意义的空白。此方法不执行 DTD 或架构验证。

  (3)Save方法

  该方法将XML数据保存到一个XML文件或是一个流对象、一个TextReader对象、一个XmlReader对象导入XML数据。

  3.XmlDocument类的事件

    XmlDocument类的事件及说明如下表所示。

                       表   XmlDocument类的事件及说明

事件

说明

NodeChanged

当属于该文档的节点的 Value已被更改时发生

NodeChanging

当属于该文档的节点的 Value 将被更改时发生

NodeInserted

当属于该文档的节点已被插入另一个节点时发生

NodeInserting

当属于该文档的节点将被插入另一个节点时发生

NodeRemoved

当属于该文档的节点已被从其父级移除时发生

NodeRemoving

当属于该文档的节点将被从文档中移除时发生

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325559182&siteId=291194637