home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / ValidationReadingXML.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  7.7 KB  |  206 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...Apply validation when reading XML?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to apply validation when reading XML data using the XMLTextReader class. Validation is the process of enforcing rules on the XML
  9. content either via a Document Type Definition (DTD) or a schema. At it's simplest, a schema is an XML representation of a DTD, but can also infer relationships 
  10. within the XML data.<br clear="left"><br>
  11. This sample follows on from the <a href="/quickstart/howto/doc/Xml/ReadXmlFile.aspx">How do I...Read XML from a file?</a> topic.
  12. </div>
  13.  
  14. <h4>Applying validation when reading XML</h4> 
  15. <Acme:SourceRef
  16. ViewSource="/quickstart/howto/samples/Xml/ValidationReadingXml/ValidationReadingXML.src"
  17. RunSample="/quickstart/howto/samples/Xml/ValidationReadingXml/ValidationReadingXML.aspx" 
  18. Icon = "/quickstart/images/genicon.gif"
  19. Caption="ValidationReadingXML.aspx"
  20. runat="server" />
  21.  
  22. <br clear="left"><br>
  23. <div class="indent" style="width:660">
  24. In this sample we are going to apply validation when reading and parsing XML data using sample files which pass and fail.
  25. The XmlTextReader can provide either DTD or XDR parse time validation. The <b>Validation</b> property can be used to request a particular validation option.
  26. XDR is the Microsoft XML-Data Schema (which is a pre-cursor to the W3C XML schema) and is called the reduced set of XML Schema (also referred to as
  27. XML-Data Reduced). This is described at <A target=_top href="http://www.ltg.ed.ac.uk/~ht/XMLData-Reduced.htm">XML-Data Reduced</A>.
  28. The table below shows the types of validation which can be set.</div>
  29.  
  30. <br clear="left"><br>
  31. <DIV class=indent>
  32. <TABLE class=table 
  33. style="border-style: solid" 
  34. width="509">
  35. <TBODY>
  36. <TR>
  37.     <TH width="179">Auto</TH>
  38.     <TH width="308">This is the default.</TH>
  39. </TR>
  40.   <tr>
  41.     <td width="179" height="17"><font size="1">Auto</font></td>
  42.     <td width="308" height="17"><font size="1">This is the default.</font></td>
  43.   </tr>
  44.   <tr>
  45.     <td width="179" height="19"><font size="1">None</font></td>
  46.     <td width="308" height="19"><font size="1">No parse time validation.</font></td>
  47.   </tr>
  48.   <tr>
  49.     <td width="179" height="19"><font size="1">DTD</font></td>
  50.     <td width="308" height="19"><font size="1">Whether to validate according to a DTD.</font></td>
  51.   </tr>
  52.   <tr>
  53.     <td width="179" height="19"><font size="1">Schema</font></td>
  54.     <td width="509" height="19"><font size="1">Whether to validate according to an XDR schema.</font></td>
  55.   </tr>
  56.   </TBODY></TABLE></DIV>
  57.  
  58. <br clear="left"><br>
  59. <div class="indent" style="width:660">
  60. The Auto mode does the following:
  61. <LI>If there is no DTD or schema then it will parse the XML without validation. 
  62. <LI>If there is a DTD defined in a <!DOCTYPE ...> declaration then it will load the DTD and process the DTD declarations.
  63. <LI>If there is no <!DOCTYPE ...> declaration but XDR schemas are specified (using the "x-schema:" prefix), then it loads and process those schemas. </LI></UL></DIV>
  64.  
  65. <div class="indent" style="width:660">
  66. In the sample code 
  67. <a href="/quickstart/util/srcview.aspx?path=/quickstart/howto/samples/Xml/ValidationReadingXml/ValidationReadingXML.src">View Source</a>
  68. four files are validated. The first BooksDTD.xml validates against the books.dtd, the second BooksDTDFail.xml fails validation against the books.dtd,
  69. the third BooksSchema.xml validates against the schema.xml file and the fourth BooksFailSchema.xml fails validation against the schema.xml.
  70. Schema.xml is an XDR schema. To set the required type of validation, the <b>Validation</b> property is set either with the value of <b>DTD</b>
  71. or <b>Schema</b> as illustrated below.</div>
  72.  
  73. <div class="code"><xmp>
  74. XmlTextReader m_reader = new XmlTextReader (m_Document1);
  75. m_reader.Validation = Validation.Schema;
  76. m_reader.Validation = Validation.DTD;
  77. </xmp></div>
  78.  
  79. <div class="indent" style="width:660">
  80. Errors in validation result in the <b>ValidationCallback</b> method being called. This property is set with a <b>ValidationDelegate</b> class before reading the
  81. XML document. If this is not provided then no validation errors are returned and parsing only stops if an XML well-formedness error occurs. However, if no
  82. ValidationDelegate is provided you get default attributes and entities without having to deal with validation errors. The sample source code illustrates this.</div>
  83.  
  84. <div class="code"><xmp>
  85. private void Validate()
  86. {
  87.     try
  88.     {
  89.         // Set the validation callback
  90.         m_reader.ValidationCallback = new ValidationDelegate(this.ValidationCallback);
  91.         // Read XML data
  92.         while (m_reader.Read()){}
  93.         Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful" : "failed"));
  94.     }
  95.     catch (XmlException e)
  96.     {
  97.         Console.WriteLine ("XmlException: " + e.ToString());
  98.     }
  99.  
  100.     catch (Exception e)
  101.     {
  102.         Console.WriteLine ("Exception: " + e.ToString());
  103.     }
  104. }
  105.  
  106. public void ValidationCallback (int errorid, String reason)
  107. {
  108.     m_success = false;
  109.     Console.Write("\r\n\tValidation error: " + reason);
  110.     if (m_reader.LineNumber > 0)
  111.     {
  112.         Console.WriteLine("Line: "+ m_reader.LineNumber + " Position: " + m_reader.LinePos);
  113.     }
  114. }
  115. </xmp></div>
  116.  
  117. <div class="indent" style="width:660">
  118. In this code sample, the class m_success variable indicates the validation state. The parser does not stop for any kind of validation error and only stops if
  119. there is a well-formedness error during the read. This feature enables you find all the validation errors in one pass without having to continously re-parse
  120. the XML.</div>
  121.  
  122. <br clear="left"><div class="indent" style="width:660">
  123. The output from the sample is shown below, illustrating DTD and XDR schema successful and failed validations. If you examine the files you can 
  124. determine the validation failures. e.g. The first book in the booksDTDFail.xml file does not have a title, which is required.</div>
  125.  
  126. <div class="code">
  127. <xmp>
  128. Validating XML file booksDTD.xml with DTD file books.dtd ...
  129. Validation finished. Validation successful
  130.  
  131. Validation XML file booksDTDFail.xml with DTD file books.dtd ...
  132.  
  133.         Validation error: Invalid content. Expecting 'title'.Line: 6 Position: 12
  134.  
  135.         Validation error: Content is incomplete.Line: 12 Position: 2
  136.  
  137.         Validation error: Content is incomplete.Line: 12 Position: 2
  138. Validation finished. Validation failed
  139.  
  140. Validation XML file booksSchema.xml with schema file schema.xml ...
  141. Validation finished. Validation successful
  142.  
  143. Validation XML file booksSchemaFail.xml with schema file schema.xml ...
  144.  
  145.         Validation error: Missing required attribute 'genre'.Line: 4 Position: 4
  146.  
  147.         Validation error: Invalid content. Expecting 'title'.Line: 4 Position: 12
  148.  
  149.         Validation error: Content is incomplete.Line: 4 Position: 4
  150.  
  151.         Validation error: Content is incomplete.Line: 4 Position: 4
  152. Validation finished. Validation failed
  153. </xmp>
  154. </div>
  155.  
  156. <H4>Summary</H4>
  157. <OL>
  158. <LI>Validation can either be against a DTD or an XDR schema, the type required being set by the Validation property.
  159. <LI>Validation is performed during the read and parsing operations.
  160. <LI>The ValidationCallback property must be set with a ValidationDelegate class to receive notification of validation errors.
  161. <LI>Validation errors do not stop parsing. Parsing only stops if there is a well-formedness error. This enables all errors to be discovered in a single pass.
  162. </LI></OL>
  163.  
  164. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  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.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.