locked
Biztalk schema validation helper class issue RRS feed

  • Question

  • Hi Friends,

    I developed the c sharp Validation Helper class.Which is validating the XML with XSD.It works fine,But if we drop the 500 messages it will fail one message 499 process.If we resubmit 1 message it will process again.Some issue with code.

    Helper class looks like that:

      public static void Validate(string xml, SchemaBase sb) {
                XmlSchemaSet xss = new XmlSchemaSet();
                foreach (var schema in sb.SchemaCollection) xss.Add(schema);
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ValidationType = ValidationType.Schema;
                settings.Schemas = xss;
                StringBuilder list = new StringBuilder();
                settings.ValidationEventHandler += (s, e) => list.AppendLine(e.Message);

                StringReader itemStringReader = new StringReader(xml);
                XmlReader itemXmlReader = XmlReader.Create(itemStringReader, settings);
                while (itemXmlReader.Read()) ;
                if (list.Length > 0) throw new XmlSchemaValidationException(list.ToString());
            }

    Schema XSD:

      <?xml version="1.0" encoding="utf-16" ?>
    - <xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:msdata="http://microsoft.com/wsdl/types/" xmlns:erp="http://erp.SAP.pgm.abc.com/Order/V1.0" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://erp.SAP.pgm.abc.com/Order/V1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    - <xs:simpleType msdata:DataType="System.Guid" name="guid">
    - <xs:restriction base="xs:string">
      <xs:pattern value="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})|(\{[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\})" />
      </xs:restriction>
      </xs:simpleType>
    - <xs:complexType name="OrderBOMLineType">
    - <xs:annotation>
      <xs:documentation>A complex type to represent all of the data pertinent to a abc Manufacturing Execution System (MES) related to a Bill Of Material Item on a Process Order.</xs:documentation>
      </xs:annotation>
    - <xs:sequence>
    - <xs:element name="BOMItemNumber">
    - <xs:annotation>
      <xs:documentation>BOM Item Number This represents the line number on the Bill of Material, associated with the Order. .</xs:documentation>
      </xs:annotation>
    - <xs:simpleType>
    - <xs:restriction base="xs:string">
      <xs:maxLength value="4" />
      </xs:restriction>
      </xs:simpleType>
      </xs:element>
    - <xs:element name="MaterialNumber" type="erp:SAPMaterialNumberType">
    - <xs:annotation>
      <xs:documentation>Material Number This is the material number of the material item to be consumed / produced. .</xs:documentation>
      </xs:annotation>
      </xs:element>
      <xs:element name="BOMQuantity" type="erp:QuantityType" />
    - <xs:element minOccurs="0" name="AlternateItemGroup">
    - <xs:annotation>
      <xs:documentation>Alternate Item Group An alternate item group is used to group one or more material items on a bill of material. For two BOM material items that share the same alternate item group id, the items should be viewed as interchangable. Example: 55</xs:documentation>
      </xs:annotation>
    - <xs:simpleType>
    - <xs:restriction base="xs:string">
      <xs:maxLength value="2" />
      </xs:restriction>
      </xs:simpleType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
    - <xs:simpleType name="UOMType">
    - <xs:restriction base="xs:string">
      <xs:enumeration value="CYL" />
      <xs:enumeration value="DR" />
      <xs:enumeration value="EA" />
      <xs:enumeration value="GM" />
      <xs:enumeration value="KG" />
      <xs:enumeration value="KGA" />
      <xs:enumeration value="LTR" />
      <xs:enumeration value="RO" />
      <xs:enumeration value="GL" />
      <xs:enumeration value="FT3" />
      </xs:restriction>
      </xs:simpleType>
    - <xs:simpleType name="SAPMaterialNumberType">
    - <xs:annotation>
      <xs:documentation>A simple type to describe an SAP Material Number.</xs:documentation>
      </xs:annotation>
    - <xs:restriction base="xs:string">
      <xs:maxLength value="18" />
      </xs:restriction>
      </xs:simpleType>
    - <xs:complexType name="QuantityType">
    - <xs:sequence>
      <xs:element name="Value" type="xs:double" />
      <xs:element name="UnitOfMeasure" type="erp:UOMType" />
      </xs:sequence>
      </xs:complexType>
    - <xs:complexType name="OrderPhaseType">
    - <xs:sequence>
      <xs:element name="PhaseNumber" type="xs:string" />
      <xs:element name="Name" type="xs:string" />
      <xs:element name="ControlKey" type="xs:string" />
      <xs:element name="AccountingQuantity" type="erp:QuantityType" />
    - <xs:element minOccurs="0" name="OrderBillOfMaterialItems">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element maxOccurs="unbounded" name="OrderBillOfMaterialItem" type="erp:OrderBOMLineType" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
    - <xs:element name="Order">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="OrderNumber" type="xs:string" />
      <xs:element name="SAPItemCode" type="erp:SAPMaterialNumberType" />
      <xs:element name="PVID" type="xs:string" />
      <xs:element name="FacilityUID" type="erp:guid" />
      <xs:element name="SAPBatchID" type="xs:string" />
      <xs:element name="PlannedQuantity" type="erp:QuantityType" />
      <xs:element name="DateCreated" type="xs:dateTime" />
      <xs:element name="StartDateTime" type="xs:dateTime" />
      <xs:element name="EndDateTime" type="xs:dateTime" />
    - <xs:element name="Operations">
    - <xs:complexType>
    - <xs:sequence>
    - <xs:element maxOccurs="unbounded" name="Operation">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="OperationNumber" type="xs:string" />
      <xs:element name="Name" type="xs:string" />
      <xs:element name="ControlKey" type="xs:string" />
      <xs:element name="AccountingQuantity" type="erp:QuantityType" />
      <xs:element name="EquipmentName" type="xs:string" />
    - <xs:element minOccurs="0" name="Phases">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element maxOccurs="unbounded" name="Phase" type="erp:OrderPhaseType" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:sequence>
      <xs:attribute name="type" type="xs:string" use="required" />
      </xs:complexType>
      </xs:element>
      </xs:schema>

    Can you any help the schema validation against the XSD,Validate method.

    THanks for Help

    HM


    hk

    Saturday, May 16, 2015 10:52 AM

Answers

All replies

  • So, why are you validating in a helper class?  BizTalk Server ships with a Validation component in the box.
    Saturday, May 16, 2015 4:39 PM
    Moderator
  • Hi hkmd79 

    Are you trying to do a bulk validation? I would try using parallel foreach loop. 

    Tuesday, May 19, 2015 6:03 PM
  • Hi,

    If you just want to validate the incoming message against any XSD, why don't you use XML Disassembler or Validator component.

    Normally XML Disassembler pipeline component only performs structure validation and not data validation, but if you want it to perform data validation make "Validate Document" property = True. Refer below blog.

    How to validate incoming Messages for XML Structure and Data Type using existing XMLReceive Pipeline ?

    If you have some justification for using C# code you can use XMLDocument.Validate Method as explained below

    XmlDocument.Validate Method (ValidationEventHandler)


    Thanks,
    Prashant
    ----------------------------------------
    Please mark this post accordingly if it answers your query or is helpful.

    • Marked as answer by Angie Xu Thursday, May 28, 2015 1:55 AM
    Wednesday, May 20, 2015 3:11 AM