; izcode.net - Validate
Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

izcode.net - Validate

VIEWS: 64 PAGES: 18

  • pg 1
									Kiểm tra tài liệu XML
             ThS. Trần Minh Triết




                                    1
                     XmlValidatingReader


 Lớp đối tượng XmlValidatingReader:
public XmlValidatingReader(XmlReader);
public XmlValidatingReader(Stream, XmlNodeType, XmlParserContext
public XmlValidatingReader(string, XmlNodeType, XmlParserContext);


 Ví dụ:
XmlTextReader _coreReader = new XmlTextReader (fileName);
XmlValidatingReader reader=new XmlValidatingReader (_coreReader);




                                                            2
                 Kiểm tra toàn bộ tài liệu XML

private bool ValidateDocument (string fileName)
{
   // Initialize the validating reader
   XmlTextReader _coreReader = new
   XmlTextReader(fileName);
   XmlValidatingReader reader = new
   XmlValidatingReader(_coreReader);
   // Prepare for validation
   reader.ValidationType = ValidationType.Auto;
   reader.ValidationEventHandler += new
      ValidationEventHandler(MyHandler);
   // Parse and validate all the nodes in the document
   while (reader.Read()) {}
   // Close the reader
   reader.Close();
   return true;
}
                                                         3
              Kiểm tra toàn bộ tài liệu XML (tt)

private void MyHandler ( object sender, ValidationEventArgs e)
{
   // Logs the error that occurred
   PrintOut(e.Exception.GetType().Name, e.Message);
}




                                                                 4
                    Các loại kiểm tra

 Validation Type
    None

    Auto

    DTD

    Schema

    XDR




                                        5
        Phân tích cách xác định loại kiểm tra phù hợp

string GetActualValidationType
        (XmlValidatingReader reader, string filename)
{ string realValidationType = "";
   if (reader.ValidationType == ValidationType.Auto)
   { if (reader.NodeType == XmlNodeType.DocumentType)
          realValidationType = "Auto.DTD";
      else
      { if(reader.SchemaType is XmlSchemaType)
          { XmlSchemaType xst = (XmlSchemaType)
                                      reader.SchemaType;
            string xsd = Path.GetFileName(xst.SourceUri);
            string doc = Path.GetFileName(filename);
            if (xsd == doc)
               realValidationType = "Auto.Schema.Inline";
            else
               realValidationType = "Auto.Schema.Ref ("+ xsd + ")";
        }
      }
   }
   return realValidationType;                                  6
Quy trình kiểm tra




                     7
                  Kiểm tra một đoạn XML

 public XmlValidatingReader
       (Stream, XmlNodeType, XmlParserContext);
 public XmlValidatingReader
       (string, XmlNodeType, XmlParserContext);




                                                  8
                      Giới thiệu về DTD

<?xml version="1.0" ?>
<!DOCTYPE class SYSTEM "class.dtd">
<!-- Sample XML document (data_dtd.xml) using a DTD -->

<class title="Applied XML Programming for .NET"
   company="DinoEsposito's Own Company"
   author="Dino Esposito">
   <days total="5" expandable="true">
     <day id="1">XML Core Classes</day>
     <day id="2">Related Technologies</day>
     <day id="3">XML and ADO.NET</day>
     <day id="4" optional="true">XML and Applications</day>
     <day id="5" optional="true">XML Interoperability</day>
   </days>
</class>


                                                              9
                      Giới thiệu về DTD

<!ELEMENT class (days)>
<!ATTLIST class title CDATA #REQUIRED
   author CDATA #IMPLIED
   company CDATA #IMPLIED>

<!ENTITY % Boolean "true | false">

<!ELEMENT days (day*)>
<!ATTLIST days total CDATA #REQUIRED
   expandable (%Boolean;) #REQUIRED>

<!ELEMENT day (#PCDATA)>
<!ATTLIST day id CDATA #REQUIRED
   optional (%Boolean;) #IMPLIED>

                                          10
                      Kiểm tra với DTD

XmlTextReader _coreReader = new
  XmlTextReader("data_dtd.xml");
XmlValidatingReader reader = new
  XmlValidatingReader(_coreReader);
reader.ValidationType = ValidationType.DTD;
reader.ValidationEventHandler += new
   ValidationEventHandler(MyHandler);
while (reader.Read());
---------------------------------------------------
private void MyHandler(object sender, ValidationEventArgs e)
{    if (e.Severity == XmlSeverityType.Error)
      { // Handle the schema exception
      }
}
                                                           11
                      Giới thiệu về XDR

<?xml version="1.0" ?>
<!-- Sample XML document (data_xdr.xml) using XDR -->

<class xmlns="x-schema:Schema.xml"
      title="Applied XML Programming for .NET"
      company="Wintellect"
      author="DinoE">
   <days total="5" expandable="true">
     <day id="1">XML Core Classes</day>
     <day id="2">Related Technologies</day>
     <day id="3">XML and ADO.NET</day>
     <day id="4" optional="true">XML and Applications</day>
     <day id="5" optional="true">XML Interoperability</day>
   </days>
</class>


                                                              12
                        Giới thiệu về XDR

<?xml version="1.0"?>

<Schema name="MyClass"
      xmlns="urn:schemas-microsoft-com:xml-data"
      xmlns:dt="urn:schemas-microsoft-com:datatypes">

<!-- Attribute Types -->
<AttributeType name="title" dt:type="string" />
<AttributeType name="company" dt:type="string" />
<AttributeType name="author" dt:type="string" />
<AttributeType name="total" dt:type="int" />
<AttributeType name="expandable" dt:type="enumeration"
             dt:values="true false" />
<AttributeType name="optional" dt:type="enumeration"
             dt:values="true false" />
<AttributeType name="id" dt:type="int" />

                                                         13
                      Giới thiệu về XDR

<!-- Element Types -->

<!-- CLASS -->
<ElementType name="class" content="eltOnly" model="closed"
   order="seq">
   <element type="days" minOccurs="1" maxOccurs="1" />
   <attribute type="title" required="yes" />
   <attribute type="author" required="no" />
   <attribute type="company" required="no" />
</ElementType>




                                                             14
                       Giới thiệu về XDR

<!-- DAYS -->
<ElementType name="days" content="eltOnly">
   <element type="day" minOccurs="1" maxOccurs="*" />
   <attribute type="total" required="yes" />
   <attribute type="expandable" required="no" />
</ElementType>

<!-- DAY -->
<ElementType name="day" content="textOnly">
   <attribute type="id" required="yes" />
   <attribute type="optional" required="no" />
</ElementType>
</Schema>

                                                        15
                       Kiểm tra với XDR

XmlTextReader _coreReader = new
  XmlTextReader("data_xdr.xml");
XmlValidatingReader reader = new
  XmlValidatingReader(_coreReader);
reader.ValidationType = ValidationType.XDR;
reader.ValidationEventHandler += new
   ValidationEventHandler(MyHandler);
while (reader.Read());
---------------------------------------------------
private void MyHandler(object sender, ValidationEventArgs e)
{    if (e.Severity == XmlSeverityType.Error)
      { // Handle the schema exception
      }
}

                                                           16
                   Giới thiệu về Schema

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="address" type="AddressType" />
  <xs:complexType name="AddressType">
     <xs:sequence>
       <xs:element name="street" type="xs:string" />
       <xs:element name="number" type="xs:string" />
       <xs:element name="city" type="xs:string" />
       <xs:element name="state" type="xs:string" />
       <xs:element name="zip" type="xs:string" />
     </xs:sequence>
     <xs:attribute name="country" type="xs:string" />
  </xs:complexType>
</xs:schema>

                                                          17
                     Kiểm tra với Schema

XmlTextReader _coreReader = new
  XmlTextReader("data_xdr.xml");
XmlValidatingReader reader = new
  XmlValidatingReader(_coreReader);
reader.ValidationType = ValidationType.Schema;
reader.ValidationEventHandler += new
   ValidationEventHandler(MyHandler);
while (reader.Read());
---------------------------------------------------
private void MyHandler(object sender, ValidationEventArgs e)
{    if (e.Severity == XmlSeverityType.Error)
      { // Handle the schema exception
      }
}

                                                           18

								
To top