-- 作者:zguilin
-- 发布时间:4/16/2008 10:28:00 AM
-- 为何这样的XML都能够通过Schema的验证?
Schema.xsd文件: <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://SchemaRevLevel360" xmlns="http://SchemaRevLevel360"> <xsd:element name="Loader"> <xsd:complexType> <xsd:choice maxOccurs="unbounded" minOccurs="1"> <xsd:element maxOccurs="unbounded" minOccurs="1" name="PartyRecords" type="PartyRecords"/> <xsd:element maxOccurs="unbounded" minOccurs="1" name="VariableRateRecords" type="VariableRateRecords"/> </xsd:choice> </xsd:complexType> </xsd:element> <xsd:complexType name="VariableRateRecord"> <xsd:choice maxOccurs="unbounded" minOccurs="0"> <xsd:element name="Loan" type="xsd:string"/> <xsd:element name="Debt" type="xsd:string"/> <xsd:element name="ExternalSystemReferenceID" type="xsd:string"/> </xsd:choice> </xsd:complexType> <xsd:complexType name="SinkScheduleRecord"> <xsd:choice maxOccurs="unbounded" minOccurs="0"> <xsd:element name="Bond" type="xsd:string"/> <xsd:element name="PaymentType" type="xsd:string"/> <xsd:element name="ExternalSystemReferenceID" type="xsd:string"/> </xsd:choice> </xsd:complexType> <xsd:complexType name="VariableRateRecords"> <xsd:choice maxOccurs="unbounded" minOccurs="0"> <xsd:element name="VariableRateRecord" type="VariableRateRecord"/> <xsd:element name="SinkScheduleRecord" type="SinkScheduleRecord"/> </xsd:choice> </xsd:complexType> <xsd:simpleType name="PartyRecordsKeyFieldType"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="NameSort"/> </xsd:restriction> </xsd:simpleType> <xsd:complexType name="PartyRecordsKeyValueType"> <xsd:simpleContent> <xsd:extension base="xsd:string"> <xsd:attribute default="NameSort" name="KeyName" type="PartyRecordsKeyFieldType"/> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:complexType name="AccountAdministratorNewType"> <xsd:choice maxOccurs="unbounded" minOccurs="0"> <xsd:element minOccurs="0" name="NameLine1" nillable="true" type="xsd:string"/> <xsd:element minOccurs="1" name="NameSort" nillable="true" type="xsd:string"/> </xsd:choice> </xsd:complexType> <xsd:complexType name="AccountAdministratorUpdateType"> <xsd:choice maxOccurs="unbounded" minOccurs="0"> <xsd:element minOccurs="0" name="NameLine1" nillable="true" type="xsd:string"/> <xsd:element minOccurs="0" name="NameSort" nillable="true" type="xsd:string"/> <xsd:element minOccurs="1" name="KeyValue" type="PartyRecordsKeyValueType"/> <xsd:element minOccurs="0" name="ExternalSystemReferenceID" type="xsd:string"/> </xsd:choice> </xsd:complexType> <xsd:complexType name="PartyRecords"> <xsd:choice maxOccurs="unbounded" minOccurs="1"> <xsd:element minOccurs="1" name="AccountAdministrator_New" type="AccountAdministratorNewType"/> <xsd:element name="AccountAdministrator_Update" type="AccountAdministratorUpdateType"/> </xsd:choice> </xsd:complexType> </xsd:schema> SchemaTest.xml内容: <?xml version="1.0" encoding="utf-8" ?> <GenevaLoader> <VariableRateRecords> <VariableRateRecord> <Loan>Loan Test</Loan> <Debt>Debt Test</Debt> <ExternalSystemReferenceID>112232</ExternalSystemReferenceID> </VariableRateRecord> <SinkScheduleRecord> <Bond>Bond Test</Bond> <PaymentType>After Got</PaymentType> <ExternalSystemReferenceID>112231</ExternalSystemReferenceID> </SinkScheduleRecord> </VariableRateRecords> <Test> <AccountAdministrator_New> <NameLine1>zgl</NameLine1> <NameSort>Joel</NameSort> </AccountAdministrator_New> <AccountAdministrator_New> <NameLine1>zgl</NameLine1> </AccountAdministrator_New> <AccountAdministrator_Update> <NameLine1>suntao</NameLine1> <NameSort>Sam</NameSort> <KeyValue> <KeyName> NameSort </KeyName> </KeyValue> <ExternalSystemReferenceID>112233</ExternalSystemReferenceID> </AccountAdministrator_Update> </Test> </GenevaLoader> 我写了一段程序,使用.NET2005对XML进行验证,看其是否满足Schema的定义,可是无论我怎么改Schema,结果都是成功的,哪位可以指导下? 程序如下: private void button1_Click(object sender, EventArgs e) { try { // Create the XmlSchemaSet class. XmlSchemaSet sc = new XmlSchemaSet(); // Add the schema to the collection. sc.Add(null, "Schema.xsd"); // Set the validation settings. XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = sc; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); // Create the XmlReader object. XmlReader reader = XmlReader.Create("SchemaTest.xml", settings); // Parse the file to Validate it against the schema. int index = 0; while (reader.Read()) { index++; } MessageBox.Show("Validation Sucessful! "+ index.ToString()); //TODO: Further validation can be done to ensure that each Test has a unique Name attribute within the TestSet } catch (XmlException ex) { // Not implemented: Write to _logFileName; MessageBox.Show("Caught XML validation error:"+ex.Message); //throw new NotImplementedException("Caught XML validation error: " + ex.Message); } } // Display any validation errors. private static void ValidationCallBack(object sender, ValidationEventArgs e) { MessageBox.Show("Caught XML validation error:" + e.Message); //throw new ApplicationException("Validation Error: " + e.Message); }
|