473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

.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 3667
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

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

Similar topics

2
by: Steve | last post by:
Hi, I have xml data containing "line item" elements and a "footer" element which contains control totals for the line items. While I can easily validate the control totals in xslt, I have not...
9
by: brandon | last post by:
I want to speed up the validation of some of my fairly large and detailed XML files. In particular there is a set of child elements and attributes that does not to be validated after it has...
2
by: Shone | last post by:
I would like to perform a 2-pass XML reading from a stream. Once using the Validating reader, just to confirm the validity against the schema, and next time to do a reading to extract the data....
0
by: Ricky Li | last post by:
I went through some article to see how I can add schema validation for my templated control by adding an XSD file into the VS.NET schema folder. Everything works fine for a schema like this: ...
1
by: billa1972 | last post by:
Hi, I am trying to hook into Yellow Freight's rating webservice. Below is the wsdl. When i try and create a proxy file with wsdl.exe i get the following errors, see below. Also, when i...
0
by: info | last post by:
Hi, Is it possible to include in the Schema validation file, every custom error message for each validation rules? This mean, in the same xsd file we can have the validation rules (patterns)...
5
by: wolf_y | last post by:
My question is simply: under what conditions will empty tags of the form <MOM></MOM> pass schema validation? Of course, the mirror question is: under what conditions will empty tags fail...
0
by: merryla | last post by:
Hi, I tried to perform schema validation. The code works well. But when I tried with the Sample txlife.xsd file from http://acord.org/ I got error in associating the namespace with the xsd i.e. in...
0
by: kenkejas | last post by:
Hello public, Got a problem. While generating proxy classes getting warnings: Schema Validation Errors: Error compiling schema. WSDL descriptor is public:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.