I'm fairly new to XML Schema validation. We have XML being imported that must validate against the following schema:
[HTML]
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.temp.com/ContactsImport"
xmlns="http://www.temp.com/ContactsImport" elementFormDefault="qualified">
<xs:element name="Contacts">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Contact" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="Email" type="EmailAddress" maxOccurs="1" minOccurs="1" nillable="false" />
<xs:element name="OptInType" type="OptInTypes" maxOccurs="1" minOccurs="0" nillable="false" />
<xs:element name="AudienceType" type="AudienceTypes" maxOccurs="1" minOccurs="0" nillable="false" />
<xs:element name="Html" type="xs:boolean" maxOccurs="1" minOccurs="0" nillable="false" />
<xs:element name="Notes" type="xs:string" maxOccurs="1" minOccurs="0" nillable="false" />
<xs:element maxOccurs="unbounded" name="CustomData" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" processContents="skip" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="EmailAddress">
<xs:restriction base="xs:string">
<xs:pattern value=".+?@.+?"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="OptInTypes">
<xs:restriction base="xs:string">
<xs:enumeration value="Unknown" />
<xs:enumeration value="Single" />
<xs:enumeration value="Double" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="AudienceTypes">
<xs:restriction base="xs:string">
<xs:enumeration value="Unknown" />
<xs:enumeration value="B2C" />
<xs:enumeration value="B2B" />
<xs:enumeration value="B2M" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
[/HTML]
I'm using the .net 2 XmlReaderSettings to specify the schema and create an XmlReader to validate the following XML:
[HTML]
<Contacts xmlns="http://schemas.dotmailer.co.uk/ContactsImport">
<Contact>
<Email>asdasd@asdasd.asd</Email>
<CustomData>
<asdasd>asdasd</asdasd>
</CustomData>
</Contact>
<Contact>
<Email>asdasd@asdasd</Email>
<CustomData>
<weqweqwe>asdasd</weqweqwe>
</CustomData>
</Contact>
</Contacts>
[/HTML]
And i'm getting the following warning message:
The element 'Contacts' in namespace 'http://www.temp.com/ContactsImport' has invalid child element 'Contact' in namespace 'http://www.temp.com/ContactsImport'. List of possible elements expected: 'Contact'.
How can this be possible?? The schema appears to be correct, VS2005 validates the xml correctly but the following code does not:
Expand|Select|Wrap|Line Numbers
- XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
- xmlReaderSettings.CheckCharacters = true;
- xmlReaderSettings.CloseInput = true;
- xmlReaderSettings.Schemas.Add(schemaNamespace, schemaUri);
- xmlReaderSettings.ValidationType = ValidationType.Schema;
- xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
- xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
- try
- {
- //Wrap the creation of the XmlReader in a 'using' block since
- //it implements IDisposable
- using (XmlReader xmlReader = XmlReader.Create(xml, xmlReaderSettings))
- {
- if (xmlReader != null)
- {
- while (xmlReader.Read())
- {
- //empty loop - if Read fails it will raise an error via the
- //ValidationEventHandler wired to the XmlReaderSettings object
- }
- //explicitly call Close on the XmlReader to reduce strain on the GC
- xmlReader.Close();
- }
- }
- // if any erros have been reported return true
- return (ValidationError.Length == 0);
- }
- catch (Exception ex) // various types of Exceptions can be thrown by the XmlReader.Create() method)
- {
- ValidationError += ex.Message;
- return false;
- }
Also we have no control to the XML's being imported, ideally this code should also validate XML's that don't have a namespace declared.
If anyone could help me on this one, i would be extremely grateful...
Kind Regards,
Pedro