Create an XML file using the XMLTextWriter object

zhaozj2021-02-16  56

Create an XML file using the XMLTextWriter object

Introduction With the popularity of XML and how to create, delete, modify the XML file is also important. A simple concept is that the XML file is nothing difference with the big text file, and it is the developer who appears in .NET, many ASP developers, usually use response.write when he needs to output an XML file. () The method is output as an XML document. Using Response.Write () to output an XML document, not a good way, first, when we use this method to output characters to form an XML file, we will worry that the output characters are not in line with XML. Specification, XML documents that do not meet the XML specification will not be able to get the true and complete display, such as: <,>, & ", and", when they appear in the XML file, we must manually find these unqualified regulations Character; again, when we need to output an XML file containing many namespaces, attributes, and elements, the code must be redundable and readable by using the response.write () method. Fortunately, .NET Framework provides a class -system.xml.xmlTextWriter for creating an XML file, using this class to create an XML file, you don't need to worry about whether the output meets the problem of XML specification, while the code will It became very simple. In this article, we will in-depth tell how to use the XMLTextWriter class to create an XML file.

About XML Description This article assumes a certain XML basis if you just contact XML, I suggest that you first look at "What is XML" and "XML start" before you continue to read this article.

XMLTextWriter Object Description: The XMLTextWriter object contains many methods that can be used to add elements and properties to XML files when creating an XML file, compared to: WriteStartDocument () - Creating an XML file first requires this method, it is Create the first row code for the XML file, used to specify the file is an XML file and set its encoding type; WriteStartElement (String) - This method is to create a new element in the XML file, you can set an element via the string parameter. Name (Of course, you can also specify an optional parameter using the optional keyword); WriteElementString (name, text_value) - If you need to create an element that except characters, what is not nested element, you can This method can be used; WriteEndelement () - corresponding to the WriteStartElement (String) method, the end of an element; WriteEndDocument () - XML ​​file is created after the completion is completed; Close () - Close all text streams, put the created XML The file is output to the specified location.

Use the XMLTextWriter object to create an XML file, you need to specify the type of file in the class constructor, and the encoding type must be system.text.encoding, such as: system.text.encoding.ascii, system.text.encoding.unicate and system.text .Encoding.utf8, which type is specified in the XMLTextWriter class constructor, and outputs the XML file in the form of streaming file. Create a simple XML file using the XMLTextWriter object Next, we demonstrate how to use the XMLTextWriter object to create a simple XML document and save it to the specified location, this XML file will contain information about the user who accesses the file, its output format is as follows: URL referrer info User agent referrer info languages ​​info Visitor's IP Address RAW URL Requested Select this XML file with this structure as output object It is necessary to use all previously spent methods here to be convenient.

The following is the ASP.NET code required to create the XML file: <% @ Import namespace = "system.xml"%> <% @ Import namespace = "system.text"%>