469,646 Members | 1,339 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

.net XML Schema validation issues

Hi,

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
  1. XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
  2.             xmlReaderSettings.CheckCharacters = true;
  3.             xmlReaderSettings.CloseInput = true;
  4.  
  5.             xmlReaderSettings.Schemas.Add(schemaNamespace, schemaUri);
  6.             xmlReaderSettings.ValidationType = ValidationType.Schema;
  7.             xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
  8.             xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
  9.  
  10.             try
  11.             {
  12.                 //Wrap the creation of the XmlReader in a 'using' block since
  13.                 //it implements IDisposable
  14.                 using (XmlReader xmlReader = XmlReader.Create(xml, xmlReaderSettings))
  15.                 {
  16.                     if (xmlReader != null)
  17.                     {
  18.                         while (xmlReader.Read())
  19.                         {
  20.                             //empty loop - if Read fails it will raise an error via the 
  21.                             //ValidationEventHandler wired to the XmlReaderSettings object
  22.                         }
  23.  
  24.                         //explicitly call Close on the XmlReader to reduce strain on the GC
  25.                         xmlReader.Close();
  26.                     }
  27.                 }
  28.  
  29.                 // if any erros have been reported return true
  30.                 return (ValidationError.Length == 0);
  31.             }
  32.             catch (Exception ex) // various types of Exceptions can be thrown by the XmlReader.Create() method)
  33.             {
  34.                 ValidationError += ex.Message;
  35.                 return false;
  36.             }
  37.  
N.B: ValidationError is a string property that gets updated with the error messages on the ValidationCallBack method.

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
Jul 17 '07 #1
2 3532
jkmyoung
2,057 Expert 2GB
Technically, VS2005 is validating incorrectly, and what you're using now is invalidating correctly.
If you can't change what's coming in, is it possible to change the namespace on your schema to match?
Jul 17 '07 #2
Technically, VS2005 is validating incorrectly, and what you're using now is invalidating correctly.
If you can't change what's coming in, is it possible to change the namespace on your schema to match?
The XML might not even have a schema! that is the issue and even with the correct namespace the code is not validating... It throws that message!...

So you say VS2005 has a bug?

Regards,
P.
Jul 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Steve | last post: by
9 posts views Thread by brandon | last post: by
1 post views Thread by billa1972 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.