I am trying to validate an XML via an XSD in c# code. However it keeps throwing an XmlSchemaValidationException, namely "The 'refname' attribute is not declared.".
The code doing the validation:
XmlReaderSettings xmlSettings = new XmlReaderSettings();
xmlSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
xmlSettings.Schemas.Add(null, @"\\[Network-drive path]\KVSchemaMod.xsd");
xmlSettings.ValidationType = ValidationType.Schema;
xmlSettings.DtdProcessing = DtdProcessing.Parse;
xmlSettings.ValidationFlags = XmlSchemaValidationFlags.AllowXmlAttributes;
xmlSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema;
xmlSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
XmlReader reader = XmlReader.Create(@"\\[Network-drive path]\KV_Article.xml", xmlSettings);
// Parse the file.
while (reader.Read()) ;
The XSD (with repetitous parts removed):
<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xs:annotation>
<xs:appinfo>
<sql:relationship name="ProductIdentifier"
parent="tblKVProduct"
parent-key="record_reference"
child="tblKVProductIdentifier"
child-key="record_reference" />
... (More SQL-mappings)
<xs:element name="ONIXMessage" sql:is-constant="1">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Product" sql:relation="tblKVProduct">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="RecordReference" sql:field="record_reference" >
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
...(More elements under Product)
The XML to be validated (ONIX-standard):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix- international.dtd">
<ONIXMessage>
<Product>
<RecordReference>0786606274</RecordReference>
<NotificationType>03</NotificationType>
<ProductIdentifier>
<ProductIDType>01</ProductIDType>
<IDTypeName>Kustantajan tuotenumero</IDTypeName>
<IDValue>95535</IDValue></ProductIdentifier>
<ProductIdentifier>
<ProductIDType>02</ProductIDType>
<IDValue>0786606274</IDValue></ProductIdentifier>
<ProductIdentifier>
<ProductIDType>03</ProductIDType>
<IDValue>9780786606276</IDValue>
</ProductIdentifier>
...(And more Product-fields)
The XML also has a Header tag as the first child of ONIXMessage (followed by all Product tags that as also childrens of ONIXMessage), but since none of the fields there are referenced in the schema I don't see how they could cause this particular error.
As I googled the error I read up on the XmlSchemaElement.RefName property (MSDN Definition) it even explicitly says that "The value cannot be set if the containing element is the schema element." Since that is the case for the XSD I don't understand why it throws an error about it not being declared.
I've been trying to find out if this could be related to the lack of namespace, but in my mind that shouldn't be a problem since there is none in neither the XML nor the XSD. Or does one need to force a namespace onto the root-element in order to validate the XML?
The validating code does work with another set of XML/XSD (very different build-up on those though), could that just be a fluke though and the error actually in the way I validate?
Just for completion's sake, the Header tag in the XML:
<Header>
<FromCompany></FromCompany>
<FromPerson></FromPerson>
<FromEmail></FromEmail>
<AddresseeIdentifier>
<AddresseeIDType></AddresseeIDType>
<IDTypeName></IDTypeName>
<IDValue></IDValue>
</AddresseeIdentifier>
<ToCompany></ToCompany>
<ToPerson></ToPerson>
<MessageNumber></MessageNumber>
<SentDate></SentDate>
<MessageNote></MessageNote>
</Header>