日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

XSD文件详解

發布時間:2024/8/26 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 XSD文件详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

具體可參考:

http://blog.csdn.net/evanerv0079/archive/2008/06/05/2515313.aspx

<?xmlversion="1.0"encoding="gb2312"?>
<studentlist>
<studentid="A101">
<name>李華</name>
<sex>男</sex>
<birthday>1978.9.12</birthday>
<score>92</score>
<skill>Java</skill>
<skill>Oracle</skill>
<skill>CSharp</skill>
<skill>SQLServer</skill>
</student>
<studentlist>

<?xmlversion="1.0"encoding="utf-8"?>
<xs:schemaid="原子類型"targetNamespace="http://student.com"elementFormDefault="qualified"
xmlns="http://student.com"xmlns:mstns="http://student.com"xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:elementname="student">
<xs:complexType>
<xs:sequence>
<xs:elementname="name"type="nameType"/>
<xs:elementref="age"/>
<xs:elementref="sex"/>
<xs:elementref="phone"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:simpleTypename="nameType">
<xs:restrictionbase="xs:string">
<xs:minLengthvalue="4"/>
<xs:maxLengthvalue="8"/>
</xs:restriction>
</xs:simpleType>

<xs:elementname="age">
<xs:simpleType>
<xs:restrictionbase="xs:int">
<xs:minInclusivevalue="1"/>
<xs:maxInclusivevalue="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:elementname="sex">
<xs:simpleType>
<xs:restrictionbase="xs:string">
<xs:enumerationvalue="男"/>
<xs:enumerationvalue="女"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

<xs:elementname="phone">
<xs:simpleType>
<xs:restrictionbase="xs:string">
<xs:patternvalue="\d{3}-\d{8}"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>

MSDN上面一個例子:

<!--booksSchema.xml-->

<?xmlversion='1.0'?>
<!--Thisfilerepresentsafragmentofabookstoreinventorydatabase-->
<bookstorexmlns="schema.xsd">
<bookgenre="autobiography"publicationdate="1981"ISBN="1-861003-11-0">
<title>TheAutobiographyofBenjaminFranklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<bookgenre="novel"publicationdate="1967"ISBN="0-201-63361-2">
<title>TheConfidenceMan</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<bookgenre="philosophy"publicationdate="1991"ISBN="1-861001-57-6">
<title>TheGorgias</title>
<author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>

<!--schema.xsd-->

<xsd:schemaxmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="schema.xsd"
elementFormDefault="qualified"
targetNamespace="schema.xsd">

<xsd:elementname="bookstore"type="bookstoreType"/>

<xsd:complexTypename="bookstoreType">
<xsd:sequencemaxOccurs="unbounded">
<xsd:elementname="book"type="bookType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexTypename="bookType">
<xsd:sequence>
<xsd:elementname="title"type="xsd:string"/>
<xsd:elementname="author"type="authorName"/>
<xsd:elementname="price"type="xsd:decimal"/>
</xsd:sequence>
<xsd:attributename="genre"type="xsd:string"/>
<xsd:attributename="publicationdate"type="xsd:string"/>
<xsd:attributename="ISBN"type="xsd:string"/>
</xsd:complexType>

<xsd:complexTypename="authorName">
<xsd:sequence>
<xsd:elementname="first-name"type="xsd:string"/>
<xsd:elementname="last-name"type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

</xsd:schema>

<!--bookSchemaFail.xml-->

<?xmlversion='1.0'?>
<bookstorexmlns="schema.xsd">
<book>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
</book>
<bookgenre="novel">
<title>TheConfidenceMan</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<bookgenre="philosophy">
<title>TheGorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>

usingSystem;
usingSystem.Xml;
usingSystem.Xml.Schema;
usingSystem.IO;

namespaceSchemaData
{
///<summary>
///Validator的摘要說明。
///</summary>
publicclassValidator
{

privateconstStringdocument3="booksSchema.xml";
privateconstStringdocument4="booksSchemaFail.xml";
privateconstStringdocument5="schema.xsd";

privateXmlValidatingReadermyXmlValidatingReader=null;
privateXmlTextReadermyXmlTextReader=null;
privateBooleanSuccess=true;
privateString[]args={document3,document4,document5};

publicValidator()
{
//
//TODO:在此處添加構造函數邏輯
//
}

publicvoidRun()
{
  try
  {

    XmlSchemaCollectionmyXmlSchemaCollection=newXmlSchemaCollection();
    myXmlSchemaCollection.Add("schema.xsd",newXmlTextReader(args[2]));

    //用架構驗證 XML文件
    Success=true;
    Console.WriteLine();
    Console.WriteLine("正在用架構文件schema.xsd驗證XML文件booksSchema.xml...");
    myXmlTextReader=newXmlTextReader(args[0]);
    myXmlValidatingReader =newXmlValidatingReader(myXmlTextReader);
    myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
    myXmlValidatingReader.ValidationType=ValidationType.Schema;
    Validate();

    //架構驗證失敗
    Success=true;
    Console.WriteLine();
    Console.WriteLine("正在用架構文件schema.xsd驗證XML文件booksSchemaFail.xml...");
    myXmlTextReader=newXmlTextReader(args[1]);
    myXmlValidatingReader=newXmlValidatingReader(myXmlTextReader);
    myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
    myXmlValidatingReader.ValidationType=ValidationType.Schema;
    Validate();
   }
  catch(Exceptione)
  {
    Console.WriteLine("異常:"+ e.ToString());
  }

  finally
  {
    //通過XmlTextReader完成
    if(myXmlValidatingReader !=null)
      myXmlValidatingReader.Close();
    }
  }

privatevoid 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());
    }
  }

publicvoidValidationEventHandle(objectsender,ValidationEventArgs args)
{
  Success=false;

  Console.WriteLine("\t驗證錯誤:"+ args.Message);

  if(args.Severity==XmlSeverityType.Warning)
  {
    Console.WriteLine("未找到要強制驗證的架構。");
  }
elseif(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);
//}
  }

  }
  }

總結

以上是生活随笔為你收集整理的XSD文件详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。