There are two ways C can read XML
- 2020-05-09 19:10:02
- OfStack
XML role
, , , XML, XML, XML, XML, XML, Web, XML1, Web, Web, XML, XML, Web, Web, Web, Web, Web, Web The data transfer for Internet, I think, is the most attractive 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 the program to access XML.
Two models of access:
access in your application and operating XML file 1, there are two kinds of models, respectively, is to use DOM (document object model (dom), and flow model, the benefits of using DOM is that it allows editing and updating XML documents, can random access data in a document, you can use XPath query, however, DOM drawback is that it needs to be one of loading the entire document into memory, for a large document, which can cause resource problems. The flow model solves this problem well, because its access to the XML file is based on the concept of a stream, that is, only the current node in memory at any given time, but it also has its drawbacks, it is read-only, only forward, and cannot perform backward navigation in the document. Although each has its own advantages and disadvantages, we can also use both in the program to achieve the advantages and disadvantages of complementary well, hehe
1. DOM document object model operations
using System.Xml;
XmlDocument xml=new XmlDocument();// Initialize the 1 a xml The instance
xml.Load(path);// Import specified xml file
xml.Load(HttpContext.Current.Server.MapPath("~/file/bookstore.xml"));XmlNode root=xml.SelectSingleNode("/root");// The specified 1 A node
XmlNodeList childlist=root.ChildNodes;// Gets all direct child nodes under a node
XmlNodeList nodelist=xml.SelectNodes("/Root/News");// Gets a collection of sibling nodes of the same name string id=node.Attributes["id"].Value;// Gets the specified property value for the specified node
string content=node.InnerText;// Gets the text in the specified node
root.HasChildNodes;// Determine whether there are child nodes under this node
1. Properties of class XmlDocument
The properties and descriptions of the XmlDocument class are shown in the following table.
attribute
instructions
Attributes
The collection of properties of the current node
BaseURI
Base URI for the current node
ChildNodes
All child nodes of a node
DocumentElement
The root of the document
DocumentType
Node declared by DOCTYPE
FirstChild
The first child of the node
HasChildNodes
Whether there are any child nodes
Implementation
Gets the XmlImplementation object for the current document
InnerText
The node contains all the text content
InnerXml
All XML contents contained by the node
IsReadOnly
Is the current node read-only
Item
Gets the specified child element
LastChild
The last one is a child node
LocalName
Gets the local name of the node
Name
Gets the qualified name of the node
NamespaceURI
Gets the namespace URI for the node
NameTable
Gets XmlNameTable associated with this implementation
NextSibling
Gets the node immediately following that node
NodeType
Gets the type of the current node
OuterXml
Gets a tag that represents this node and all its children
OwnerDocument
Gets XmlDocument to which the current node belongs
ParentNode
Gets the parent of the node (for nodes that can have a parent)
Prefix
Gets or sets the namespace prefix for the node
PreserveWhitespace
Gets or sets a value indicating whether to leave white space in the element content
PreviousSibling
Gets the node immediately before the node
SchemaInfo
Returns the post-schema validation information set for the node (PSVI)
Schemas
Gets or sets the XmlSchemaSet object associated with this XmlDocument
Value
Gets or sets the value of the node
XmlResolver
Set XmlResolver for parsing external resources
2. Methods of class XmlDocumentThe methods and instructions for the XmlDocument class are shown in the following table.
methods
instructions
AppendChild
Adds the specified node to the end of the list of child nodes of the node
CreateAttribute
Creates XmlAttribute with the specified name
CreateCDataSection
Creates XmlCDataSection that contains the specified data
CreateComment
Creates XmlComment that contains the specified data
CreateDocumentFragment
Create XmlDocumentFragment
CreateDocumentType
Returns the new XmlDocumentType object
CreateElement
Create XmlElement
CreateEntityReference
Creates XmlEntityReference with the specified name
CreateNavigator
Create a new XPathNavigator object to navigate the document
CreateNode
Create XmlNode
CreateProcessingInstruction
Create an XmlProcessingInstruction with the specified name and data
CreateSignificantWhitespace
Create an XmlSignificantWhitespace node
CreateTextNode
Creates XmlText with the specified text
CreateWhitespace
Create one XmlWhitespace node
CreateXmlDeclaration
Creates an XmlDeclaration node with the specified value
GetElementById
Gets XmlElement with the specified ID
GetElementsByTagName
Returns an XmlNodeList containing a list of all elements that match the specified name
GetNamespaceOfPrefix
Finds the xmlns declaration closest to the given prefix in the current node range and returns the namespace URI in the declaration
GetPrefixOfNamespace
Finds the xmlns declaration closest to 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 the 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
Loads the specified XML data
LoadXml
Loads the XML document from the specified string
Normalize
Convert all XmlText nodes to "normal" form
PrependChild
Adds the specified node to the beginning of the list of child nodes for that node
ReadNode
Create an XmlNode object based on the information in XmlReader . The reader must be positioned on a node or property
RemoveAll
Removes all child nodes and/or properties of the current node
RemoveChild
Removes the specified child node
ReplaceChild
Replace the old node with the new one
Save
Save the XML document to the specified location
SelectNodes
Select the list of nodes that match the XPath expression
SelectSingleNode
Select the first XmlNode to match the XPath expression
Supports
Test whether the DOM implementation implements specific functionality
Validate
Verify that XmlDocument is not the XML schema definition language (XSD) schema included in the Schemas property.
WriteContentTo
Save all child levels of the XmlDocument node to the specified XmlWriter
WriteTo
Save the XmlDocument node to the specified XmlWriter
introduces the common methods below.
(1) Load method
The method can import XML data from an XML file specified by a string, or from a stream object, an TextReader object, or an XmlReader object.
(2) LoadXml method
The method performs the import of XML data from a specific XML file. By default, the LoadXml method retains neither whitespace nor meaningful whitespace. This method does not perform DTD or schema validation.
(3) Save method
this method saves XML data to an XML file or imports XML data into a stream object, an TextReader object, and an XmlReader object.
3. Events of the XmlDocument classThe events and instructions for the XmlDocument class are shown in the following table.
XmlDocument events and instructions
The event
instructions
NodeChanged
Occurs when the Value belonging to the node of the document has been changed
NodeChanging
Occurs when the Value belonging to the node of the document is about to be changed
NodeInserted
Occurs when a node belonging to the document has been inserted into another node
NodeInserting
Occurs when a node belonging to the document will be inserted into another node
NodeRemoved
Occurs when a node belonging to the document has been removed from its parent
NodeRemoving
Occurs when a node belonging to the document is to be removed from the document
http://kb.cnblogs.com/page/42226/