XML file operation guide

Source: Internet
Author: User
Tags xslt xslt processor


1. XML Introduction
The full name of XML is eXtensible Markup Language (eXtensible Markup Language). Its syntax is similar to HTML, and tags are used to describe data. HTML labels are fixed. They can only be used and cannot be modified. XML labels are different. They can be used without pre-defined labels. Instead, they are defined based on the design requirements. XML is a metalanguage that can derive many protocols and specifications based on different industries and semantics.
XML mainly provides two object models. They are:
(1) The Document Object Model (DOM) class is the memory representation of XML documents. DOM allows you to read, operate, and modify XML documents programmatically. The XmlReader class also reads XML, but it provides non-cached inbound and read-only access. This means that XmlReader cannot edit attribute values or element content, or insert or remove nodes. Editing is the main function of DOM. XML data representation in memory is a common structured method, although the actual XML data is stored in a linear manner when it is in a file or when it is passed in from another object.
(2) architecture Object Model (SOM) in System. xml. the Schema namespace provides a set of classes that correspond to and fully comply with the WWW Federation (W3C) XML Schema Recommendation (XML Schema recommendations ). These classes enable you to read the architecture from the file, or programmatically create architectures in the memory that can be compiled, verified, or written to the file.
This document introduces the Object Model (DOM.

Ii. XML namespace and related class overview
. The. Net Framework provides the following namespaces: System. xml, System. xml. schema, System. xml. serialization, System. xml. xpath and System. xml. xsl to include classes related to XML operations. In this article, we will mainly discuss the System. Xml namespace. For other related content, please refer to the relevant materials.
System. the Xml namespace contains a variety of XML classes that can analyze, verify, and operate XML data using readers, writers, and components that comply with the W3C DOM requirements. The following list contains the main classes in the XML namespace:
XmlNode is an important abstract class. Every node in the DOM tree should be sent by it.
XmlDocument class implements W3C Document Object Model Level 1 core and core DOM Level 2.
The XmlTextReader class provides fast, non-cache, and inbound read-only access to XML data.
The XmlNodeReader class provides XmlReader for the given DOM node subtree.
XmlValidatingReader class provides DTD, XDR, and XSD architecture verification.
The XmlTextWriter class provides a fast and only-in method for generating XML.
The XmlDataDocument class provides the implementation of XmlDocument that can be associated with a dataset. You can view and operate structured XML data in the relational representation of a dataset or the tree representation of XmlDataDocument.
The XPathDocument class provides XSLT with a fast and high-performance cache for XML document processing.
The XPathNavigator class provides a cursor style model for the W3C XPath 1.0 data model used for storage for browsing.
The XslTransform class is an XSLT processor compatible with W3C XSLT 1.0 specifications and is used to convert XML documents.
The XmlSchema object model class provides a set of class that can be browsed. These classes directly reflect the W3C XSD specification. They provide the ability to create an XSD architecture programmatically.
The XmlSchemaCollection class provides the XDR and XSD architecture libraries. These cache-in-memory architectures provide XmlValidatingReader with a fast, analytical verification XmlReader class.

Iii. XML Document Object Model (DOM)
. NET only supports the xml dom mode, but does not support the SAX mode. The Document Object Model (DOM) class is the memory representation of the XML document. The representation of XML data in the memory is structured. Generally, the tree structure is used to represent the DOM object model. To have a more intuitive understanding of the DOM memory structure, Let's first look at the structure of the XML data in the memory in the next example.
& Lt; xml version = "1.0" & gt;
<Book>
<Author> Jiang Xin </author>
<Price> 40 </price>
</Book>
1. Read XML data
The xml dom mode allows you to read XML data in multiple ways, such as from a stream, URL, text reader, or XmlReader. The Load method reads documents into the memory and contains the overload methods that can be used to obtain data from different formats. The following code demonstrates how to read XML data from multiple XmlReader readers.
// Create the validating reader and specify DTD validation.
TxtReader = new XmlTextReader (filename );
Reader = new XmlValidatingReader (txtReader );
Reader. ValidationType = ValidationType. DTD;
// Set a handler to handle validation errors.
Reader. ValidationEventHandler + = eventHandler;
// Pass the validating reader to the XML document.
// Validation fails due to an undefined attribute, but
// Data is still loaded into the document.
XmlDocument doc = new XmlDocument ();
Doc. Load (reader );
Console. WriteLine (doc. OuterXml );
In addition, there is a simple LoadXML method that reads XML directly from strings, such:
// Create an XmlDocument object.
XmlDocument doc = new XmlDocument ();
// Read XML data.
Doc. LoadXml ("<book> <author> biqing </author> <price> 40 </priec> </book> ");
2. Access the attributes in the DOM
Another important feature in XML documents is attribute, which indicates a feature of an element. Next we will learn how to access the attributes of elements. In DOM mode, if the current node is an element, you can use the HasAttribute method to check whether an attribute exists. If so, you can use the XmlElement. Attributes attribute to obtain a set containing all Attributes of the element. Generally, you can follow the steps below to access the attributes of an element:
// Obtain the attribute set of the book node element.
XmlAttributeCollection attrs = myxml. DocumentElement.
SelectSingleNode ("// book"). Attributes;
// Extract the property ID from the property set.
XmlAttribute attr = attrs ["ID"];
// Obtain the value of the property ID.
String id = atrr. Value;
3. Create a node in the DOM
You can insert a new node into the DOM tree to add new elements to the XML document. The XmlDoument class provides methods for creating all types of nodes, such as CreateElement and CreateTextNode. After creating a new node, you can use several methods to insert it into the DOM tree.
The following code creates an element node:
// Create an Element node and insert it into the child node of the book node.
XmlElement elem = doc. CreateElement ("ISDN ");
// Create a Text node and use it as the value of ISDN.
XmlText text = doc. CreateTextNode ("CN94-0000/TP ");
There are several ways to insert a node into the DOM tree:
InsertBefore: indicates that it is inserted before the referenced node. For example, insert a new node in Location 5:
XmlNode refChild = node. ChildNodes [4];
Node. InsertBefore (newChild, refChild );
InsertAfter: indicates that the referenced node is inserted.
AppendChild: The End Of The subnode list that adds a node to a given node.
PrependChild: indicates the beginning of the list of child nodes added to a given node.
Append: Append the XmlAttribute node to the end of the attribute set associated with the element.
The following code inserts the above two nodes into the Child Nodes of the book node:
// Insert the elem node to the last subnode of the book node.
Doc. DocumentElement. AppendChild (elem );
// Add the text node as the elem node value.
Doc. DocumentElement. LastChild. AppendChild (text );
4. Delete nodes in the DOM
To delete a node in the DOM, you can use the RemoveChild method. For example, to delete multiple nodes from the DOM, you can directly call the RemoveAll method, which deletes all sublevels and attributes of the current node.
For example:
XmlDocument doc = new XmlDocument ();
Doc. LoadXml ("<book genre = 'novel' ISBN = '1-861001-57-5 '>" +
"<Title> Pride And Prejudice </title>" +
"</Book> ");
XmlNode root = doc. DocumentElement;
// Delete the title element node.
Root. RemoveChild (root. FirstChild );
If you only delete the attributes of an element node, you can use the following three methods:
XmlAttributeCollection. Remove: deletes a specific attribute.
XmlAttributeCollection. RemoveAll: deletes all attributes in the set.
XmlAttributeCollection. RemoveAt: deletes an attribute in the set by using the index number.
For example:
XmlDocument doc = new XmlDocument ();
Doc. LoadXml ("<book genre = 'novel' ISBN = '1-861001-57-5 '>" +
"<Title> Pride And Prejudice </title>" +
"</Book> ");
XmlAttributeCollection attrColl = doc. DocumentElement. Attributes;
// Delete the genre attribute.
AttrColl. Remove (attrColl ["genre"]);
5. Save the XML document
After the application effectively modifies the XML document, you need to save the content in the memory to the XML file on the disk. It is easy to Save the XML document. Call the Save method.
The code below demonstrates the use of the Save method:
Using System;
Using System. Xml;
Public class Sample {
Public static void Main (){
// Create the XmlDocument.
XmlDocument doc = new XmlDocument ();
Doc. LoadXml ("<item> <name> wrench </name> </item> ");
// Add a price element.
XmlElement newElem = doc. CreateElement ("price ");
NewElem. InnerText = "10.95 ";
Doc. DocumentElement. AppendChild (newElem );
// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter ("data. xml", null );
Writer. Formatting = Formatting. Indented;
Doc. Save (writer );
}
}
 
4. Access XML documents using XmlReader
XmlReader is an abstract class that provides non-cached, read-only access to XML documents. XmlReader is similar to how our desktop applications extract data from databases. Database Service returns a cursor object that contains all query result sets and returns a reference to the start address of the target dataset. The client of the XmlReader receives a reference pointing to the reader instance. This instance extracts the underlying data streams and presents the retrieved data as an XML tree. The reader class provides read-only and forward cursors. You can use the method provided by the reader class to scroll the cursor to traverse each piece of data in the result set.
As the abstract base class of the reader, it allows developers to customize their own types of readers. NET class library, three types of reader classes have been implemented for the application, namely, XmlTextReader, XmlValidatingReader, or XmlNodeReader class.
The reader's main function is to read the methods and attributes of XML documents. The Read method in its class is a basic method for reading XML documents. It reads nodes in XML documents as streams ). In addition, the class provides more advanced read methods such as ReadString, ReadInnerXml, ReadOuterXml, and ReadStartElement. In addition to reading XML documents, the XmlReader class also provides navigation functions such as MoveToAttribute, movetofirstattricontent, MoveToContent, MoveToFirstContent, MoveToElement, and MoveToNextAttribute.
(1) Use XmlReader
To use XMLReader, you must first create an Instance Object of the XmlReader dispatch class, such:
XmlTextReader reader = new XmlTextReader (file)
Create an XmlTextReader Reader using a file stream. After creating a reader object, it can be used as an XmlDocument. the parameter usage of the Load method (the method used to access the XML document has been described earlier). You can also directly use the reader to read the structure content of the document, the following code demonstrates how to use the XmlTextReader reader to read XML documents:
// Create an XmlTextReader class to point it to the target XML document
XmlTextReader reader = new XmlTextReader (file );
// Cyclically retrieve the node text and put it into the StringWriter object instance
StringWriter writer = new StringWriter ();
String tabPrefix = "";
While (reader. Read ())
{
// Output start flag. If the node type is element
If (reader. NodeType = XmlNodeType. Element)
{
// Add the reader. Depth tab Based on the Depth of the node where the element is located, and output the element name to <>.
TabPrefix = new string ('\ t', reader. Depth );
Writer. WriteLine ("{0 }<{ 1}>", tabPrefix, reader. Name );
}
Else
{
// Output end flag. If the node type is element
If (reader. NodeType = XmlNodeType. EndElement)
{
TabPrefix = new string ('\ t', reader. Depth );
Writer. WriteLine ("{0} </{1}>", tabPrefix, reader. Name );
}
(2) attributes of XmlReader
The XmlReader class has some attributes that can be modified during reading, and some other attributes that will not affect reading when they are changed after reading. Next we will give a brief introduction to XmlReader attributes.
AttributeCount attribute: When a derived class is overwritten, the number of attributes on the current node is obtained.
BaseURI attribute: When a derived class is overwritten, the base URI of the current node is obtained.
CanResolveEntity property: gets a value that indicates whether the reader can analyze and parse entities.
Depth attribute: When a derived class is overwritten, the Depth of the current node in the XML document is obtained.
EOF attribute: gets a value that indicates whether the reader is positioned at the end of the stream when it is overwritten in a derived class.
HasAttributes attribute: gets a value indicating whether the current node has any attributes.
HasValue attribute: gets a Value when it is overwritten in a derived class. This Value indicates whether the current node can have a Value.
IsDefault attribute: gets a value when it is overwritten in a derived class. This value indicates whether the current node is an attribute generated from the default value defined in The DTD or architecture.
IsEmptyElement attribute: gets a value when it is overwritten in a derived class. This value indicates whether the current node is an empty element (for example ).
Item property: it has been overloaded. Obtain the value of this attribute when it is overwritten in a derived class. In C #, this attribute is the indexer of the XmlReader class.
(3) Common XmlReader Methods
1. ReadInnerXml and ReadOuterXml Methods
XmlReader provides the ReadInnerXml and ReadOuterXml methods to read element and attribute content. ReadInnerXml reads all content (including tags) as strings, while ReadOuterXml reads the node and all its sub-level content (including tags ).
Assume that the XML document:
<Node>
This <child id = "123"/>
</Node>
The ReadInnerXml call returns this <child id = "123"/>, while the ReadOuterXml call returns <node> this <child id = "123"/> </node>.
2. Read Method
Read the next node from the stream. It is usually used in a loop. For example:
XmlTextReader rdr = new XmlTextReader ("book. xml ");
While (rdr. Read ())
{
.... // Read each node in sequence.
}
3. ReadAttributeValue Method
Resolve the attribute value to one or more Text, EntityReference, or EndEntity nodes. For example:
// Create the reader.
Reader = new XmlTextReader (xmlFrag, XmlNodeType. Element, context );
// Read the misc attribute. The attribute is parsed
// Into multiple text and entity reference nodes.
Reader. MoveToContent ();
Reader. MoveToAttribute ("misc ");
While (reader. ReadAttributeValue ()){
If (reader. NodeType = XmlNodeType. EntityReference)
Console. WriteLine ("{0} {1}", reader. NodeType, reader. Name );
Else
Console. WriteLine ("{0} {1}", reader. NodeType, reader. Value );
}
4. ReadString Method
Read the content of an element or text node as a string. For example:
// Load the reader with the XML file.
Reader = new XmlTextReader ("elems. xml ");
// Parse the XML and display the text content of each of the elements.
While (reader. Read ()){
If (reader. IsStartElement ()){
If (reader. IsEmptyElement)
Console. WriteLine ("<{0}/>", reader. Name );
Else {
Console. Write ("<{0}>", reader. Name );
Reader. Read (); // Read the start tag.
If (reader. IsStartElement () // Handle nested elements.
Console. Write ("\ r \ n <{0}>", reader. Name );
Console. WriteLine (reader. ReadString (); // Read the text content of the element.
}
}
5. Other Methods
MoveToElement: move to an element that contains the current attribute node.
MoveToFirstAttribute: Move to the first property.
MoveToNextAttribute: Move to the next attribute.
Skip: Skip the sublevel of the current node.
 
5. Use the XmlWriter class to write XML documents
XmlWriter is an abstract base class that defines interfaces used to write XML. XmlWriter provides only-in, read-only, and non-Cache XML Stream generation methods. More importantly, XmlWriter ensures that all XML data complies with W3C XML 1.0 recommendation specifications during design. You don't even have to worry about writing and closing tags because XmlWriter handles everything.
Like the XmlReader abstract class, an application must use a dispatch class of XmlWriter to create an instance. You can customize your own XmlWriter dispatch class. the. Net Class Library provides the XmlTextWriter dispatch class.
(1) Use XmlWriter
Before using XmlWrite, you must create a dispatch class instance of the XmlWriter abstract class, write elements, attributes, and content using the WriteXXX method of XmlWrite, and Close the XML document using the Close method.
The following code demonstrates how to use XmlWriter to write an XML document:
// Open the XML writer (use the default Character Set)
XmlTextWriter xmlw = new XmlTextWriter (filename, null );
Xmlw. Formatting = Formatting. Indented;
Xmlw. WriteStartDocument ();
Xmlw. WriteStartElement ("array ");
Foreach (string s in theArray)
{
Xmlw. WriteStartElement ("element ");
Xmlw. WriteAttributeString ("value", s );
Xmlw. WriteEndElement ();
}
Xmlw. WriteEndDocument ();
// Close the writer
Xmlw. Close ();
(2) attributes of the XmlWriter class
The XmlWriter class provides the following attributes:
WriteState attribute: gets the writer status when it is overwritten in a derived class.
XmlLang attribute: When a derived class is overwritten, The XmlSpace that represents the current xml: space Range is obtained.
XmlSpace attribute: gets the current xml: lang range when it is overwritten in a derived class.
(3) Common XmlWriter Methods
1. WriteXXX Method
This is a series of write operations, mainly including:
WriteBase64: encode the specified binary byte into Base64 and write the result text.
WriteBinHex: encode the specified binary bytes into BinHex and write the result text.
WriteCData method: Write the <! [CDATA [...]> block.
WriteCharEntity method: forces the generation of character entities for the specified Unicode character value.
WriteChars method: writes text in a buffer every time.
WriteComment method: write comments containing the specified text <! --... -->. ,
WriteDocType: Write the DOCTYPE declaration with the specified name and optional attributes.
The code below demonstrates the WriteChars method usage. The usage of other methods is similar.
// Handling surrogate pair implements SS buffer streams.
Char [] charArray = new char [4];
Char lowChar, highChar;
Random random = new Random ();
LowChar = Convert. ToChar (random. Next (0xDC01, 0 xDFFF ));
HighChar = Convert. ToChar (random. Next (0xD801, 0 xDBFF ));
XmlTextWriter tw = new XmlTextWriter ("test. xml", null );
Tw. WriteStartElement ("Root ");
CharArray [0] = 'a ';
CharArray [1] = 'B ';
CharArray [2] = 'C ';
CharArray [3] = highChar;
Try
{
Tw. WriteChars (charArray, 0, charArray. Length );
}
Catch (Exception ex ){
}
Array [0] = highChar;
Array [1] = lowChar;
CharArray [2] = 'D ';
Tw. WriteChars (charArray, 0, 3 );
Tw. WriteEndElement ();
2. Flush Method
This method is used to refresh all content in the buffer to the base stream and refresh the base stream at the same time. The following code demonstrates the use of the Flush method.
XmlTextWriter writer = new XmlTextWriter (Console. Out );
// Use indenting for readability
Writer. Formatting = Formatting. Indented;
// Write an XML fragment.
Writer. WriteStartElement ("book ");
Writer. WriteElementString ("title", "Pride And Prejudice ");
Writer. WriteEndElement ();
Writer. Flush ();
// Write another XML fragment.
Writer. WriteStartElement ("cd ");
Writer. WriteElementString ("title", "Americana ");
Writer. WriteEndElement ();
Writer. Flush ();
// Close the writer.
Writer. Close ();
3. Close Method
This method disables the Document Stream and basic stream after the write operation of the XML document is completed.
 
6. A complete XML file read/write instance
This article uses the "books. xml" file attached to the VS.net development tool as an example to introduce the basic operations of C # On XML files. Assume that the path of the books. xml file is C: \ books. xml.
The file information is as follows:
The "books. xml" file is as follows:
<Xml version = '1. 0'>
<! -- This file represents a fragment of a book store inventory database -->
<Bookstore>
<Book genre = "autobiography" publicationdate = "1981" ISBN = "1-861003-11-0">
<Title> The Autobiography of Benjamin Franklin </title>
<Author>
<First-name> Benjamin </first-name>
<Last-name> Franklin </last-name>
</Author>
<Price> 8.99 </price>
</Book>
<Book genre = "novel" publicationdate = "1967" ISBN = "0-201-63361-2">
<Title> The Confidence Man </title>
<Author>
<First-name> Herman </first-name>
<Last-name> Melville </last-name>
</Author>
<Price> 11.99 </price>
</Book>
<Book genre = "philosophy" publicationdate = "1991" ISBN = "1-861001-57-6">
<Title> The Gorgias </title>
<Author>
<First-name> Sidas </first-name>
<Last-name> Plato </last-name>
</Author>
<Price> 9.99 </price>
</Book>
</Bookstore>

1. Methods for reading XML files
The XmlReader class provides APIs for XML analysis, and XmlTextReader is designed to process byte streams. In general, if you need to access XML as raw data, you can use XmlTextReader to avoid DOM overhead. Saving DOM access can speed up XML reading. For example, an XML document may have a header section that is used to pass the document for processing elsewhere. XmlTextReader has different constructors to specify the location of XML data.
XmlTextReader reader = new XmlTextReader ("books. xml ");
After loading, XmlTextReader uses the Read method to move in XML data and retrieves the next record from the document in sequence. If no record exists, the Read method returns false.
While (reader. Read ())
{
// Do some work here on the data
...
}
Do While (reader. Read ())
'Do some work here on the data
...
Loop
To process XML data, each record has a node type that can be determined from the NodeType attribute. After the NodeType enumeration returns the node type, this example tests the node type to see whether it is an element type or a document type. If the node is either of the two types, this example uses the Name and Value attributes to process the node to display details about the node. The Name attribute returns the node Name (such as the element and attribute Name), and the Value Attribute returns the node Value (node text) of the current node (record ).
While (reader. Read ())
{
Switch (reader. NodeType)
{
Case XmlNodeType. Element: // The node is an Element
Console. Write ("<" + reader. Name );
While (reader. MoveToNextAttribute () // Read attributes
Console. Write ("" + reader. Name + "= '" + reader. Value + "'");
Console. Write ("> ");
Break;
Case XmlNodeType. DocumentType: // The node is a DocumentType
Console. WriteLine (NodeType + "<" + reader. Name + ">" + reader. Value );
Break;
...
}
}
Do While (reader. Read ())
Select Case reader. NodeType
Case XmlNodeType. Element 'the node is an Element
Console. Write ("<" + reader. Name)
While (reader. MoveToNextAttribute () 'read attributes
Console. Write ("" + reader. Name + "= '" + reader. Value + "'")
End while
Console. Write ("> ")
Case XmlNodeType. DocumentType 'the node is a DocumentType
Console. WriteLine (NodeType & "<" & reader. Name & ">" & reader. Value );
...
End Select
Loop

2. How to Write an XML file
First, create an Instance Object of the XmlTextWriter class. The constructor XmlTextWriter of this class has three reloads. Its parameters are a string, a stream object, and a TextWriter object. Here we use the string parameter form to specify the location of the XML file to be created. The method is as follows:
XmlTextWriter textWriter = New XmlTextWriter ("C: \ books. xml", null );
After the object is created, we call the WriterStartDocument method to write the XML document. The following example describes how to use these methods to write the XML document.
Using System;
Using System. Xml;
Namespace WriteXML
{
Class Class1
{
Static void Main (string [] args)
{
// Create an Instance Object of the XmlTextWriter class
XmlTextWriter textWriter = new XmlTextWriter ("C :\\ myXmFile. xml", null );
// Start the write process and call the WriteStartDocument Method
TextWriter. WriteStartDocument ();
// Write description
TextWriter. WriteComment ("First Comment XmlTextWriter Sample Example ");
TextWriter. WriteComment ("myXmlFile. xml in root dir ");
// Write an element
TextWriter. WriteStartElement ("Name ","");
TextWriter. WriteString ("Student ");
TextWriter. WriteEndElement ();
// Write another element
TextWriter. WriteStartElement ("Address ","");
TextWriter. WriteString ("Colony ");
TextWriter. WriteEndElement ();
// Write characters
Char [] ch = new char [3];
Ch [0] = 'a ';
Ch [1] = 'R ';
Ch [2] = 'C ';
TextWriter. WriteStartElement ("Char ");
TextWriter. WriteChars (ch, 0, ch. Length );
TextWriter. WriteEndElement ();
// When the document writing ends, call the WriteEndDocument method.
TextWriter. WriteEndDocument ();
// Disable textWriter
TextWriter. Close ();
}
}
}
 
 
VII. Summary
This article introduces C #'s knowledge about compiling XML files, as well as the XML namespace, XML Document Object Model (DOM), and related classes provided in. Net. Based on the introduction in this article, I believe that readers have gained an intuitive understanding of these two aspects. This is the main purpose of this Article.
 

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.