当前位置:网站首页>Introduction to XML III

Introduction to XML III

2022-07-04 13:41:00 Hua Weiyun

Xml Basics 03

Supplementary review :

Xml

Extensible markup language

eXtensible Markup Language

A way to store data in simple text format

Xml**** structure

Elements , attribute , Statement , notes

XML**** Document validation

DTD

Schema :XSD framework

Document object model -DOM

XML The tree structure of the file

Xml**** Data analysis

Dom:XPath

Sax:XmlTextReader, XmlTextWriter

Xml And Ado.Net Interaction

XmlNode**** How to select nodes

SelectSingleNode(string)

Choose a match XPath The first of the expressions XmlNode.

SelectNodes(string)

Choose a match XPath The node list of the expression

Apply to :

XmlDocument

XmlElement

XmlAttribute

XPath

Xml Document query language

For from XML Select node set in document

The goal is ( Suppose the current node is book node )XPath give an example
Select the current node .
Select the parent node
Select all child nodes of the current node *****
choice author Child node author
Select the... Of the current node title attribute @title
Select all the attributes of the current node @*
Select all text nodes of the current node (XmlText)text()
Select all nodes with a specific name in the current document , For example, select all author node //author
Select all nodes with a specific name and a specific parent node name in the current document , For example, selecting all parent nodes is book, Node is author The node of //book/author
Select the node whose value meets the condition , For example, the author selected is Wu Chengen book node **//book[author= Wu chengen
Select the node whose attribute meets the condition , For example, choose yes title It's the Red Mansion book node **//book[@title= ‘ A dream of red mansions ’]

Xml analysis

DOM : Document based ( object ), Tree structure , When loading, the whole load ( Large memory consumption )

SAX : Based on flow , Load line by line when loading ( It is suitable for parsing a large amount of data )

SAX Pattern parsing Xml

The main constituent classes :

XmlReader( contain XmlTextReader and XmlNodeReader Two classes )

  • Only forward , Non cached readers , take XML Data is processed as a stream .
  • The memory demand is not great
  • There is no need for flexible navigation
  • Not based on DOM Read and write functions available in the model

XmlWriter ( contain Xml)

  • Is a build for XML Stream provides “ Only forward , Non cache ” The abstract class of the method .

XmlTextReader: Provide for the right to XML Fast file , Only forward access .XmlTextReader Our technology is similar to reading any other file , Open the file first , Read the file , Until the end of the file , Close the file after .

XmlTextReader newReader = new XmlTextReader("filename");newReader.Read();	// Read xml The next node of the file newReader.Read().XmlNodeType()// Find what you need 

XmlNodeType Member of an enumeration :

become member say bright
Attribute attribute , Such as id=“1”
CommentXML notes , Such as <!- - Some comment - ->
Document Document object , Express XML The root of the tree
XmlDeclaration At the top of the document XML Statement
Element , EndElement Start and end elements
Text The text content of the element
Whitespace Space between marks
…………
…………

Use XmlTextWriter Write XML The process of file is the same as that of file system writing data .
1 Open or create a file .
2 Write data to a file
3 Refresh the data flow to ensure that each action is submitted
4 Close file

XmlTextWrite Use :

1 Create a xml file
XmlTextWriter bookWriter = new XmlTextWriter(“C:\book.xml", Encoding.Default);
2 Write XML
establish XML Statement
bookWriter.WriteStartDocument();
Write node :
bookWriter.WriteStartElement(“Price”);
bookWriter.WriteString(“120”);
bookWriter.WriteEndElement();
perhaps
bookWriter.WriteElementString(" Price ", “120”);
Add attribute
bookWriter.WriteStartElement(“Book”);
bookWriter.WriteAttributeString(“Title”, “ The romance of The Three Kingdoms ”);
bookWriter.WriteEndElement();
3 Refresh the data flow and close the file
bookWriter.Flush();
bookWriter.Close();

Dom And Sax difference

Document object model DOM

DOM Read the whole XML File and store the file in a tree structure .

DOM Create objects that represent everything in the original document , Including elements 、 Text 、 Properties and spaces .

DOM API It's parsing XML Documentation is a very useful method .

SAX analysis

Read and operate XML Faster data 、 A lightweight .

SAX Process it when reading a document , So you don't have to wait for the entire document to be stored before taking action .

Suitable for processing data streams , That is to process data in turn with the flow of data .

summary

Xml

Extensible markup language

eXtensible Markup Language

A way to store data in simple text format

Xml structure

Elements , attribute , Statement , notes

XML Document validation

DTD

Schema :XSD framework

Xml Data analysis

Document object model -DOM

similar Sax:XmlTextReader, XmlTextWriter

Xml And Ado.Net Interaction

DataSet

ReadXml(“xml file ”)

WriteXml(" xml file ");

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041155517556.html