473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.net XML Schema validation issues

2 New Member
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" elementFormDefa ult="qualified" >
<xs:element name="Contacts" >
<xs:complexType >
<xs:sequence>
<xs:element maxOccurs="unbo unded" name="Contact" minOccurs="1">
<xs:complexType >
<xs:sequence>
<xs:element name="Email" type="EmailAddr ess" maxOccurs="1" minOccurs="1" nillable="false " />
<xs:element name="OptInType " type="OptInType s" maxOccurs="1" minOccurs="0" nillable="false " />
<xs:element name="AudienceT ype" type="AudienceT ypes" maxOccurs="1" minOccurs="0" nillable="false " />
<xs:element name="Html" type="xs:boolea n" maxOccurs="1" minOccurs="0" nillable="false " />
<xs:element name="Notes" type="xs:string " maxOccurs="1" minOccurs="0" nillable="false " />
<xs:element maxOccurs="unbo unded" name="CustomDat a" 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:simpleTyp e name="EmailAddr ess">
<xs:restricti on base="xs:string ">
<xs:pattern value=".+?@.+?"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleTyp e name="OptInType s">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="Unknown" />
<xs:enumerati on value="Single" />
<xs:enumerati on value="Double" />
</xs:restriction>
</xs:simpleType>
<xs:simpleTyp e name="AudienceT ypes">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="Unknown" />
<xs:enumerati on value="B2C" />
<xs:enumerati on value="B2B" />
<xs:enumerati on value="B2M" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
[/HTML]
I'm using the .net 2 XmlReaderSettin gs to specify the schema and create an XmlReader to validate the following XML:
[HTML]
<Contacts xmlns="http://schemas.dotmail er.co.uk/ContactsImport" >
<Contact>
<Email>asdasd@a sdasd.asd</Email>
<CustomData>
<asdasd>asdas d</asdasd>
</CustomData>
</Contact>
<Contact>
<Email>asdasd@a sdasd</Email>
<CustomData>
<weqweqwe>asdas d</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 ValidationCallB ack 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 3684
jkmyoung
2,057 Recognized Expert Top Contributor
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
pnmcosta
2 New Member
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
1796
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 been able to do it using a schema, which I need to do. Example: <data> <item><fld1>1</fld2><fld2>2</fld2></item>
9
4441
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 completed a certain point in the system, but that will still be present in the file. At points later in the system, when these files are read in, I do not want to validate this section as it has already been validated much earlier, but still need the...
2
9453
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. Actually, second time I do a deserialization, the data from XML is fed directly to an object. The problem I am experiencing is the error at the second reading attempt, and error description implies that reader is winded to the end of the XML and...
0
1139
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: <mycontrolns:mycontrol id="test" runat="server"> <mytemplate>test</mytemplate> </mycontrolns> and I was able to specify the XSD to validate "mytemplate" as the only valid child type for "mycontrol". Here's a challenge: Is there anyway
1
6411
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 reference this wsdl in .NET it seems to do it fine, yet there are no objects to reference except RateQuoteBeanService. In the WSDL it looks like there should be getRateQuote, and QUOTEREQUEST, etc.
0
2159
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) and the custom error message throw during validation for the end user. I know .NET enable it using errorMessage tag, but with a standard W3C java validation ... If is it possible, does it works in IE too? Like that the same XSD file could be...
5
3685
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 validation? The former seems to be an easier question to answer. XML files will arrive from around the world and must be schema validated before further processing and loading into a database, so I'm trying to foresee the various layouts that might be...
0
1421
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 the add () method. The add () method returned E_FALSE. When I referred the MSDN library, it states that "E_FAIL is returned if: · The collection is read-only · The document is not a recognized schema
0
1701
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: http://eptstest-integra.sodra.lt/orabpel/epts/surastiAsmenis/surastiAsmenis?wsdl Have tried with Altova XML spy and SoapUI 3.5. Descriptors load well except WseWsdl3.exe tool. Using such proxy class is ok until you get fault response, then everything is translated to "business...
0
9585
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.