通过xsd验证xml合法性的例子。有验证程序,xml和xsd文件

booksSchema.xml  
   
  <?xml   version='1.0'?>  
  <!--   This   file   represents   a   fragment   of   a   book   store   inventory   database   -->  
  <bookstore   xmlns   =   "schema.xsd">  
      <book   genre="autobiography"   publicationdate="1981"   ISBN="1-861003-11-0">  
          <title>The   Autobiography   of   Benjamin   Franklin</title>  
          <author>  
              <first-name>Benjamin</first-name>  
              <last-name>Franklin</last-name>  
          </author>  
          <price>8.99</price>  
      </book>  
      <book   genre="novel"   publicationdate="1967"   ISBN="0-201-63361-2">  
          <title>The   Confidence   Man</title>  
          <author>  
              <first-name>Herman</first-name>  
              <last-name>Melville</last-name>  
          </author>  
          <price>11.99</price>  
      </book>  
      <book   genre="philosophy"   publicationdate="1991"   ISBN="1-861001-57-6">  
          <title>The   Gorgias</title>  
          <author>  
              <first-name>Sidas</first-name>  
              <last-name>Plato</last-name>  
          </author>  
          <price>9.99</price>  
      </book>  
  </bookstore>  
   
   
   
  schema.xsd  
   
  <xsd:schema   xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
          xmlns="schema.xsd"  
          elementFormDefault="qualified"  
          targetNamespace="schema.xsd">  
   
    <xsd:element   name="bookstore"   type="bookstoreType"/>  
   
    <xsd:complexType   name="bookstoreType">  
      <xsd:sequence   maxOccurs="unbounded">  
        <xsd:element   name="book"     type="bookType"/>  
      </xsd:sequence>  
    </xsd:complexType>  
   
    <xsd:complexType   name="bookType">  
      <xsd:sequence>  
        <xsd:element   name="title"   type="xsd:string"/>  
        <xsd:element   name="author"   type="authorName"/>  
        <xsd:element   name="price"     type="xsd:decimal"/>  
      </xsd:sequence>  
      <xsd:attribute   name="genre"   type="xsd:string"/>  
      <xsd:attribute   name="publicationdate"   type="xsd:string"/>  
      <xsd:attribute   name="ISBN"   type="xsd:string"/>  
    </xsd:complexType>  
   
    <xsd:complexType   name="authorName">  
      <xsd:sequence>  
        <xsd:element   name="first-name"     type="xsd:string"/>  
        <xsd:element   name="last-name"   type="xsd:string"/>  
      </xsd:sequence>  
    </xsd:complexType>  
   
  </xsd:schema>  
   
   
   
  bookSchemaFail.xml  
   
  <?xml   version='1.0'?>  
  <bookstore   xmlns="schema.xsd">  
      <book>  
          <author>  
              <first-name>Benjamin</first-name>  
              <last-name>Franklin</last-name>  
          </author>  
      </book>  
      <book   genre="novel">  
          <title>The   Confidence   Man</title>  
          <author>  
              <first-name>Herman</first-name>  
              <last-name>Melville</last-name>  
          </author>  
          <price>11.99</price>  
      </book>  
      <book   genre="philosophy">  
          <title>The   Gorgias</title>  
          <author>  
              <name>Plato</name>  
          </author>  
          <price>9.99</price>  
      </book>  
  </bookstore>  
   
  程序:  
  using   System;  
  using   System.Xml;  
  using   System.Xml.Schema;  
  using   System.IO;  
   
  namespace   SchemaData  
  {  
  ///   <summary>  
  ///   Validator   的摘要说明。  
  ///   </summary>  
  public   class   Validator  
  {  
   
  private   const   String   document3   =   "booksSchema.xml";  
  private   const   String   document4   =   "booksSchemaFail.xml";  
  private   const   String   document5   =   "schema.xsd";  
   
  private   XmlValidatingReader   myXmlValidatingReader   =   null;  
  private   XmlTextReader   myXmlTextReader   =   null;  
  private   Boolean   Success   =   true;  
  private   String[]   args   =   {document3,   document4,   document5};  
   
  public   Validator()  
  {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
  }  
   
  public   void   Run()  
  {  
  try  
  {  
   
  XmlSchemaCollection   myXmlSchemaCollection   =   new   XmlSchemaCollection();  
  myXmlSchemaCollection.Add("schema.xsd"   ,   new   XmlTextReader(args[2]));  
   
  //   用架构验证   XML   文件  
  Success   =   true;  
  Console.WriteLine();  
  Console.WriteLine("正在用架构文件   schema.xsd   验证   XML   文件   booksSchema.xml   ...");  
  myXmlTextReader   =   new   XmlTextReader   (args[0]);  
  myXmlValidatingReader   =   new   XmlValidatingReader(myXmlTextReader);  
  myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);  
  myXmlValidatingReader.ValidationType   =   ValidationType.Schema;  
  Validate();  
   
  //   架构验证失败  
  Success   =   true;  
  Console.WriteLine();  
  Console.WriteLine("正在用架构文件   schema.xsd   验证   XML   文件   booksSchemaFail.xml   ...");  
  myXmlTextReader   =   new   XmlTextReader   (args[1]);  
  myXmlValidatingReader   =   new   XmlValidatingReader(myXmlTextReader);  
  myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);  
  myXmlValidatingReader.ValidationType   =   ValidationType.Schema;  
  Validate();  
  }  
   
  catch   (Exception   e)  
  {  
  Console.WriteLine("异常:"   +   e.ToString());  
  }  
   
  finally  
  {  
  //   通过   XmlTextReader   完成  
  if   (myXmlValidatingReader   !=   null)  
  myXmlValidatingReader.Close();  
  }  
  }  
   
  private   void   Validate()  
  {  
  try  
  {  
  //   设置验证事件处理程序  
  myXmlValidatingReader.ValidationEventHandler   +=   new   ValidationEventHandler   (this.ValidationEventHandle);  
   
  //   读取   XML   数据  
  while   (myXmlValidatingReader.Read()){}  
  Console.WriteLine   ("验证已完成。验证   {0}",   (Success==true   ?   "成功"   :   "失败"));  
  }  
  catch   (XmlException   e)  
  {  
  Console.WriteLine   ("Xml   异常:"   +   e.ToString());  
  }  
   
  catch   (Exception   e)  
  {  
  Console.WriteLine   ("异常:"   +   e.ToString());  
  }  
  }  
   
  public   void   ValidationEventHandle   (object   sender,   ValidationEventArgs   args)  
  {  
  Success   =   false;  
   
  Console.WriteLine("/t验证错误:"   +   args.Message);  
   
  if   (args.Severity   ==   XmlSeverityType.Warning)  
  {  
  Console.WriteLine("未找到要强制验证的架构。");  
  }    
  else  
  if   (args.Severity   ==   XmlSeverityType.Error)  
  {  
  Console.WriteLine("验证实例文档时发生验证错误。");  
  }    
   
  if   (args.Exception   !=   null)   //   XSD   架构验证错误  
  {  
  Console.WriteLine(args.Exception.SourceUri   +   ","   +     args.Exception.LinePosition   +   ","   +     args.Exception.LineNumber);  
  }  
   
  //if   (myXmlValidatingReader.Reader.LineNumber   >   0)  
  //{  
  //         Console.WriteLine("Line:   "+   myXmlValidatingReader.Reader.LineNumber   +   "   Position:   "   +   myXmlValidatingReader.Reader.LinePosition);  
  //}  
  }  
   
  }  
  } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值