views:

25

answers:

0

Hi,

I want to validate the xml requests received in my WCF webservice. I created business classes by converting the xsd's to code with Xsd2Code.exe (Xsd2Code@Codeplex).

Q: Why does the validator not fire when Element BrancheInfo is null?

Validation functions:

    /// <summary>
    /// If a servicerequest enters our domain, we have to check whether the request is conform the xsd.
    /// </summary>
    /// <returns></returns>
    public List<ServiceError> ValidateServiceRequest(T requestMessage, string xsdFile)
    {
        //Serialize the requestmessage according to type T.
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        MemoryStream ms = new MemoryStream();
        serializer.Serialize(ms, requestMessage);
        ms.Position = 0;

        string xsdFilePath = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "ServiceTypes");
        xsdFilePath = Path.Combine(xsdFilePath, xsdFile);

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation | XmlSchemaValidationFlags.ReportValidationWarnings | XmlSchemaValidationFlags.ProcessIdentityConstraints; 
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
        settings.IgnoreComments = true;
        settings.IgnoreWhitespace = true;
        settings.Schemas.Add(null, xsdFilePath);

        try
        {
            using (XmlReader rdr = XmlReader.Create(ms, settings))
            {
                while (rdr.Read())
                {
                }
            }
        }
        catch
        {
            throw;
        }

        return ServiceErrors;
    }

The callback function to parse the error:

    /// <summary>
    /// This callback function is fired when the xml is not correct according to the xsd file.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    private void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        //Add the error to the error list.
    }

Simplified XML Schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="Header">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="BrancheInfo" minOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="BrancheInfo" nillable="0">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="BrancheGroup" minOccurs="1"/>
        <xs:element ref="Branche" minOccurs="1"/>
        <xs:element ref="SubBranche" minOccurs="0"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="BrancheGroup" nillable="0">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
  <xs:element name="Branche" nillable="0">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
  <xs:element name="SubBranche">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:minLength value="1"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>
</xs:schema>

I used 2 tools to call the webservice to test whether is wasn't a problem with the tool itself (setting elements to null):

  • WcfTestClient.exe (in my case -> C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE)
  • WCFStorm