.net XML Schema validation issues

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pnmcosta
    New Member
    • Jul 2007
    • 2

    .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" 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:

    Code:
    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;
                }
    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
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #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?

    Comment

    • pnmcosta
      New Member
      • Jul 2007
      • 2

      #3
      Originally posted by jkmyoung
      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.

      Comment

      Working...