XSD文件與xml 文件

 
XSD是指XML結構定義 ( XML Schemas Definition )
XML Schema 是DTD的替代品。XML Schema語言也就是XSD。
XML Schema描述了XML文檔的結構。可以用一個指定的XML Schema來驗證某個XML文檔,以檢查該XML文檔是否符合其要求。文檔設計者可以通過XML Schema指定一個XML文檔所允許的結構和內容,並可據此檢查一個XML文檔是否是有效的。XML Schema本身是一個XML文檔,它符合XML語法結構。可以用通用的XML解析器解析它。
  一個XML Schema會定義:文檔中出現的元素、文檔中出現的屬性、子元素、子元素的數量、子元素的順序、元素是否爲空、元素和屬性的數據類型、元素或屬性的默認和固定值。
  XSD是DTD替代者的原因,一是據將來的條件可擴展,二是比DTD豐富和有用,三是用XML書寫,四是支持數據類型,五是支持命名空間。
  XSD文件的後綴名爲.xsd。
  XML Schema的優點:
  1) XML Schema基於XML,沒有專門的語法 
  2) XML可以象其他XML文件一樣解析和處理 
  3) XML Schema支持一系列的數據類型(int、float、Boolean、date等) 
  4) XML Schema提供可擴充的數據模型。 
  5) XML Schema支持綜合命名空間 
  6) XML Schema支持屬性組。
 
 
對xml文件的操作:http://www.cnblogs.com/jhtchina/archive/2007/06/05/691610.html
 
 <!-- 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>    
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> <!-- 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>   
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><!-- 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> 
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->  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);      //}      }           }      }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章