XML reader and writer-write XML into a file

Source: Internet
Author: User
Tags xml reader
How to write XML into a file

This example illustrates how to use the xmltextwriter class to write XML into a file. The writer provides a quick and only-in method for generating XML, and helps you generate XML documents that comply with W3C Extensible Markup Language (XML) 1.0 and the namespace specifications in XML. Xmltextwriter writes data to a stream, instead of using an object model (such as xml dom), providing better performance.

Note: For more information about how to use Dom to write XML, see how to load and use xmldocument (W3C DOM)

 

VB writexmlfile. aspx

[Running example] | [View Source Code]

Generally, if you need to write XML as the raw data, you can use xmltextwriter to avoid using Dom. Xmltextwriter is an implementation of the xmlwriter class, which provides APIs for writing XML into files, streams, or textwriter. This class has many validation and check rules to ensure that the XML format is correct. When a rule conflicts, exceptions are thrown and these exceptions should be caught. Xmltextwriter has different constructors, each of which specifies the location of different types of XML data written. This example uses the constructor that writes XML into a file. Note that the following sample code uses a string to construct xmltextwriter, which represents the location of the newbooks. xml file.


XmlTextWriter myXmlTextWriter = new XmlTextWriter ("newbooks.xml", null);            
Dim myXmlTextWriter As XmlTextWriter = new XmlTextWriter ("newbooks.xml", nothing)            
C # VB  

In addition to the file name, this constructor also uses the encoding you want to generate. If encoded as "null", the writer writes the UTF-8. For more information about how to use encoding in the XML document, see W3C XML 1.0 specifications.

The next snippet of this sample code uses a single book element to create an XML file. It first uses the formatting attribute to specify the format of the XML data being written. By setting this attribute to indented, the writer uses the indentation and indentchar attributes to indent sub-elements.

myXmlTextWriter.Formatting = Formatting.Indented;            myXmlTextWriter.WriteStartDocument(false);            myXmlTextWriter.WriteDocType("bookstore", null, "books.dtd", null);            myXmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database");            myXmlTextWriter.WriteStartElement("bookstore");            myXmlTextWriter.WriteStartElement("book", null);            myXmlTextWriter.WriteAttributeString("genre","autobiography");            myXmlTextWriter.WriteAttributeString("publicationdate","1979");            myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9");            myXmlTextWriter.WriteElementString("title", null, "The Autobiography of Mark Twain");            myXmlTextWriter.WriteStartElement("Author", null);            myXmlTextWriter.WriteElementString("first-name", "Mark");            myXmlTextWriter.WriteElementString("last-name", "Twain");            myXmlTextWriter.WriteEndElement();            myXmlTextWriter.WriteElementString("price", "7.99");            myXmlTextWriter.WriteEndElement();            myXmlTextWriter.WriteEndElement();            //Write the XML to file and close the myXmlTextWriter            myXmlTextWriter.Flush();            myXmlTextWriter.Close();            
myXmlTextWriter.Formatting = System.Xml.Formatting.Indented            myXmlTextWriter.WriteStartDocument(false)            myXmlTextWriter.WriteDocType("bookstore", nothing, "books.dtd", nothing)            myXmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database")            myXmlTextWriter.WriteStartElement("bookstore")            myXmlTextWriter.WriteStartElement("book", nothing)            myXmlTextWriter.WriteAttributeString("genre","autobiography")            myXmlTextWriter.WriteAttributeString("publicationdate","1979")            myXmlTextWriter.WriteAttributeString("ISBN","0-7356-0562-9")            myXmlTextWriter.WriteElementString("title", nothing, "The Autobiography of Mark Twain")            myXmlTextWriter.WriteStartElement("Author", nothing)            myXmlTextWriter.WriteElementString("first-name", "Mark")            myXmlTextWriter.WriteElementString("last-name", "Twain")            myXmlTextWriter.WriteEndElement()            myXmlTextWriter.WriteElementString("price", "7.99")            myXmlTextWriter.WriteEndElement()            myXmlTextWriter.WriteEndElement()            'Write the XML to file and close the myXmlTextWriter            myXmlTextWriter.Flush()            myXmlTextWriter.Close()            
C # VB  

During the process of creating this element, the above sample code also shows the XML writing method corresponding to each XML node type. For example, writing an element will call the writeelementstring method, and writing an attribute will call the writeattributestring method. For the nesting level, you can use the writestartelement/writeendelement pair. to create more complex attributes, you can use the writestartattribute/writeendattribute pair.

When writing XML, pay attention to how the sample code uses the writestartdocument method to write an XML declaration with the version "1.0. If you want the writer to check whether the document format is correct (first, the XML declaration, the doctype in the preface, there is only one root-level element, and so on), you must call any other writing method before, call this optional writestartdocument method. Then, this code calls the writedoctype method to compile the document type named "Bookstore. The third parameter in the writedoctype call specifies that the writer will write system "Books. DTD ". After writing, the XML file indicates an external DTD that needs to be verified based on it.

Finally, the sample code calls the flush method to save the XML data to the file, and then calls the close method. (Although this example only requires the close method, the generated XML needs to be saved and the writer needs to be reused .)

To check the output of xmltextwriter, you can use xmltextreader to read the generated file and perform a round-trip test to verify that the XML format is correct.

Summary
  1. Xmltextwriter provides a fast and only-in method for generating XML.
  2. Xmltextwriter helps you write XML documents that comply with W3C Extensible Markup Language (XML) 1.0 specifications and namespaces specifications in XML.
  3. Xmltextwriter provides constructors that write XML into files, streams, or textwriter.
  4. Each XML node type has a corresponding XML writing method.

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.