The note of learning ASP. NET [14] reads and writes XML files using classes in the system. xml namespace in. NET Framework

Source: Internet
Author: User
Tags xml example

. NET provides excellent support for XML reading and writing, and provides many classes in the. NET Framework XML namespace to read and write XML files. When you need to read and write XML files, you can enter using system. xml at the top of the Code to tell the compiler and runtime environment that we need to use classes in this namespace.
First, review the basic information about XML:
1. XML is the Extensible Makeup Language extension markup language, which can be used to store formatted data.
2. XML is based on (elements) Elements
3. element consists of the start tag, element content, and end tag.
4. The attributes of the element can store the information of the current tag.

The following is an XML sample code snippet. Here we store information about the chapter. We store two chapters and their titles:
<Chapters Total = "2">
<Chapter> data types, variables and operators </Chapter>
<Chapter> language fundamentals </Chapter>
</Chapters>

Learn about the XML read/write related classes in. NET Framework:
1. xmlwriter class we use this class to write XML into a file or memory. Its common methods and attributes are as follows:
Create method to create a new instance, and input the name of the XML file as the parameter during the call.
The writestartdocument method outputs the definition header of XML: similar to <? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
The beginning of the output element of the writestartelement method is the <chapters Total = "2"> line in the preceding XML example.
End part of the output element of the writeendelement method </chapters>
The writeelementstring method outputs the first part of the element, the content of the element, and the end part of the element. You can output the following content at a time, <chapter> language fundamentals </Chapter>

2. xmlwritersettings class we use this class to set some format information for writing XML.
The indent attribute can be set to true or false to determine whether to use indent when an element is written into XML.
The indentchars attribute defines the delimiter used for indentation.
The newlinechars attribute defines the characters used for line feed when an element is written.

3. xmlreader class we use this class to read files or XML files in memory. Its common methods and attributes are as follows:
Create method to create a new XML file reference instance. The name of the XML file is input as the parameter during the call.
The read method reads the node of an element at a time and returns true or false.
The nodetype attribute determines whether the tag node type read by the current read method is element or text.
The readtofollowing method transmits an element name to be searched as a parameter, which is basically the same as the read method, but here we are looking for an element with a specific name.

The sample code is as follows:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. xml;
 
Namespace usingnetframework
{
Class Program
{
Static void main (string [] ARGs)
{
Static void writeandreadxml () // only one static method is defined here. Execute the following code block
{

// First, an instance of the xmlwritersettings class named settings is created to define the XML format settings output using the xmlwriter class.
Xmlwritersettings settings = new xmlwritersettings ();

// Set the indent attribute of the settings object to true, so that the indent is automatically used when xmlwriter is used to output XML.
// If this parameter is set to false, the output XML file will not have any line feed indentation or other formats
Settings. indent = true;

// Set line feed characters
Settings. newlinechars = "/R/N ";

// Call the static create method of the xmlwriter class to create a new writer object to write the XML file. If the file already exists, it will overwrite the content in it.
// Call this method to pass in two parameters. The first one is a string type, which indicates the name of the file to be created, including the full path of the location to be stored.
// The second parameter is the settings object we created above. This allows writer to output XML in the format we have defined.
Xmlwriter writer = xmlwriter. Create ("C: // test. xml", settings );
 
Writer. writestartdocument (true); // output the definition header of XML: similar to <? XML version = "1.0" encoding = "UTF-8" standalone = "yes"?>
Writer. writestartelement ("chapters"); // output the start tag of the chapters element <chapters>
// The following line of code adds an attribute to the chapters element output by the previous line of code. The actual result is <chapters Total = "2">
Writer. writeattributestring ("Total", "2 ");

// The following line of code outputs a complete element at a time. One-time output includes the element start part, element content, and element end part.
// The output result is as follows: <chapter> data types, variables and operators </Chapter>
Writer. writeelementstring ("chapter", "data types, variables and operators ");
Writer. writeelementstring ("chapter", "language fundamentals ");

// Output the end part of the chatpers element </chapters>
Writer. writeendelement ();

// End our output. The file is generated on the hard disk when we use the Create method, but the above content will be actually output to the text file only after we call the close method.
Writer. Close ();

/// The above Department shows the general method for writing XML. The following is a general method for reading XML.
// To read XML files, we use the xmlreader class. First, we need to use the Create method of xmlreader to create a new xmlreader object,
// This is actually a reference of our XML file. The parameter of the method is that the name of the file to be read includes its full path.
Xmlreader reader = xmlreader. Create ("C: // test. xml"); // escape characters are used in "C: // test. xml"
 
// Read the XML one node at a time
// A while loop is used here. If the read method of the reader object returns true, the node information in the XML file is read.
// When false is returned, all information is read. It can be understood that if you use the read method once to read data, that is, the current read Node
// Save the data to the reader object. Then, we can call the attributes of the reader object to read the information of the currently read node.
While (reader. Read ())
{
If (reader. nodetype = xmlnodetype. Text)
{
Console. writeline (reader. value );
}
}

// Use the close method to close the read stream after reading.
Reader. Close ();
 
Console. writeline ();

// The read method of xmlreader is demonstrated above. The following is an example of the readtofollowing method of the xmlreader class.
// You also need to use the Create method of xmlreader to create a new xmlreader object. Someone may ask why a new xmlreader object has been created.
// Xmlreader object, which cannot be used directly here. This is because whether the read method or the readtofollowing method is used
// The Order of XML is from top to bottom and cannot be returned to the top to re-read. Therefore, each time you need to re-read the XML file, you need to create a new xmlreader class object.
Reader = xmlreader. Create ("C: // test. xml ");

// Unlike the read method, the readtofollowing method reads the information of each node with a specific name.
// Select to read only the Element Node named chapter.
While (reader. readtofollowing ("chapter "))
{
// After reading an element node, we can use its information for operations. The content of this element is output here.
Console. writeline (reader. readinnerxml ());
}

// Close the read stream
Reader. Close ();
}
}
}
}

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.