home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / WriteXMLFile.aspx < prev    next >
Encoding:
Text File  |  2000-06-11  |  5.8 KB  |  190 lines

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" -->
  4.  
  5. <h4>How Do I...Write XML to a file?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to write XML to a file using the XmlTextWriter class.
  9. The writer provides a fast, forward only way of generating XML and helps you to build XML documents that conform to the
  10. <A href="http://www.w3.org/TR/1998/REC-xml-19980210">W3C Extensible Markup Language (XML) 1.0</A> specification 
  11. and the <A href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</A> specification.
  12. This writer provides stream write to XML rather than using an object model such as the XML DOM, and so gives better performance.
  13. See <a target=content href="DOMInterfaceXmlDocument.aspx">How Do I...Load and use the XmlDocument (W3C DOM)?</a>.</div>
  14.  
  15. <h4>Writing XML to a file</h4> 
  16. <Acme:SourceRef 
  17. ViewSource="/quickstart/howto/samples/Xml/WriteXmlFile/WriteXmlFile.src"
  18. RunSample="/quickstart/howto/samples/Xml/WriteXmlFile/WriteXmlFile.aspx" 
  19. Icon = "/quickstart/images/genicon.gif"
  20. Caption="WriteXmlFile.aspx"
  21. runat="server" />
  22.  
  23. <br clear="left"><br>
  24. <div class="indent" style="width:660">
  25. Typically the XmlTextWriter is used if you need to write XML as 'raw' data without the overhead of a DOM and therefore provides a faster mechanism for writing XML.
  26. The XmlWriter class is the API which provides the XML writing to file, stream or a TextWriter. This provides numerous validation and checking rules to ensure
  27. that the written XML is well formed. When certain violations occur exceptions are thrown, which should be caught. The XmlTextWriter has different constructors
  28. to specify the location to write the XML data to. In this sample we are going to write XML to the
  29. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/WriteXmlFile/&file=newbooks.xml">newbooks.xml</a> file.
  30. The sample code below constructs an XmlTextWriter with a string representing the file location.
  31.  
  32. <div class="code"><xmp>
  33. XmlTextWriter writer = new XmlTextWriter ("newbooks.xml", null);
  34. </xmp></div>
  35.  
  36. <div style="width:660">
  37. The constructor takes the filename that you want to write to, and the encoding that you would like to generate. If encoding is 'null' it writes out UTF-8.
  38. The XML 1.0 specification details how XML documents are encoded.</div>
  39.  
  40. <br clear="left"><br><div style="width:660">
  41. Here we are going to write another fragment of the bookstore XML file. For each XML type there are corresponding XML write methods. 
  42. For example, to write an element use the <b>WriteElementString</b> method and to write an attribute use the <b>WriteAttribute</b> method.
  43. For nested levels the <b>WriteStartElement</b>/<b>WriteEndElement</b> pair is used and equally for more complex attribute creation you can use the 
  44. <b>WriteStartAttribute</b>/<b>WriteEndAttribute</b> pair. The code sample below creates an XML file with a single book.</div>
  45.  
  46. <div class="code">
  47. <xmp>
  48. writer.Formatting = Formatting.Indented;
  49. writer.WriteXmlDecl(false);
  50. writer.WriteDocType("bookstore", null, "books.dtd", null);
  51. writer.WriteComment("This file represents another fragment of a book store inventory database");
  52. writer.WriteStartElement("bookstore");
  53.     writer.WriteStartElement("book", null);
  54.         writer.WriteAttribute("genre","autobiography");
  55.         writer.WriteAttribute("publicationdate","1979");
  56.         writer.WriteAttribute("ISBN","0-7356-0562-9");
  57.         writer.WriteElementString("title", null, "The Autobiography of Mark Twain");
  58.         writer.WriteStartElement("Author", null);
  59.             writer.WriteElementString("first-name", "Mark");
  60.             writer.WriteElementString("last-name", "Twain");
  61.         writer.WriteEndElement();
  62.         writer.WriteElementString("price", "7.99");
  63.     writer.WriteEndElement();
  64. writer.WriteEndElement();
  65.  
  66. //Write the XML to file and close the writer
  67. writer.Flush();
  68. writer.Close();
  69. </xmp></div>
  70.  
  71. <div style="width:660">
  72. First the XML declaration with the version "1.0" is written with the <b>WriteXmlDecl</b> method.
  73. This method, although optional, must be the first write call before any others writes, otherwise an exception is thrown.
  74. Next the document type is written with the name "bookstore" with the method <b>WriteDocType</b>. The 3rd parameter writes SYSTEM "books.dtd" to indicate that there is
  75. an external DTD to validate against. The <b>Formatting</b> property sets the output to be formatted. This causes child elements to be indented using the <b>Indentation</b>
  76. and <b>IndentChar</b> properties. The <b>Flush</b> method persists the XML to file (here only the <b>Close</b> method is really required, however there are occassions
  77. whereby the XML generated so far needs to be persisted and the writer reused)
  78. </div>
  79.  
  80. <br clear="left"><div style="width:660">
  81. To check the output from the XmlTextWriter, we can perform a round trip test by reading in the generated file with an XmlTextReader to validate that the XML
  82. is well formed.
  83.  
  84. <H4>Summary</H4>
  85. <OL>
  86. <LI>The XmlTextWriter provides a fast, forward only way of generating XML.
  87. <LI>The XmlTextWriter helps you to write XML documents that conform to the <A href="http://www.w3.org/TR/1998/REC-xml-19980210">W3C Extensible Markup 
  88. Language (XML) 1.0</A></A> specification and the <A><A href="http://www.w3.org/TR/REC-xml-names/">Namespaces in XML</A></A> specification. 
  89. <LI>The XmlTextWriter provides constructors to write XML to a file, a stream or a TextWriter.
  90. <LI>For each XML type there are corresponding XML write methods.
  91. </LI></OL>
  92.  
  93. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  94. </xml>
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.