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上面一個(gè)例子:
<!--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:在此處添加構(gòu)造函數(shù)邏輯
//
}
publicvoidRun()
{
try
{
XmlSchemaCollectionmyXmlSchemaCollection=newXmlSchemaCollection();
myXmlSchemaCollection.Add("schema.xsd",newXmlTextReader(args[2]));
//用架構(gòu)驗(yàn)證 XML文件
Success=true;
Console.WriteLine();
Console.WriteLine("正在用架構(gòu)文件schema.xsd驗(yàn)證XML文件booksSchema.xml...");
myXmlTextReader=newXmlTextReader(args[0]);
myXmlValidatingReader =newXmlValidatingReader(myXmlTextReader);
myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
myXmlValidatingReader.ValidationType=ValidationType.Schema;
Validate();
//架構(gòu)驗(yàn)證失敗
Success=true;
Console.WriteLine();
Console.WriteLine("正在用架構(gòu)文件schema.xsd驗(yàn)證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
{
//設(shè)置驗(yàn)證事件處理程序
myXmlValidatingReader.ValidationEventHandler+=new ValidationEventHandler(this.ValidationEventHandle);
//讀取XML數(shù)據(jù)
while(myXmlValidatingReader.Read()){}
Console.WriteLine("驗(yàn)證已完成。驗(yàn)證{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驗(yàn)證錯(cuò)誤:"+ args.Message);
if(args.Severity==XmlSeverityType.Warning)
{
Console.WriteLine("未找到要強(qiáng)制驗(yàn)證的架構(gòu)。");
}
elseif(args.Severity==XmlSeverityType.Error)
{
Console.WriteLine("驗(yàn)證實(shí)例文檔時(shí)發(fā)生驗(yàn)證錯(cuò)誤。");
}
if(args.Exception!=null)//XSD架構(gòu)驗(yàn)證錯(cuò)誤
{
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);
//}
}
}
}
總結(jié)
- 上一篇: 巴西木怎么养?巴西木育种注意事项
- 下一篇: 浅谈WebSocket协议、WS协议和W