Invalid child element 'xxx' in namespace 'http://yyy' while there is actually no extra namespace setting in xml

Viewed 344

SO had lots of similar discussions but after look into about 5-6 posts. None of them seems to match my case. From my observation, most of the previous posts appear to have extra namespace setting that results in the problem but I don't think my case has the same.

So I have a webservice that will receive the following xml

<?xml version="1.0" encoding="UTF-8"?>
<ProductBindingReportRequest xmlns="http://schemas.ms.it.oem/digitaldistribution/2010/10" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <CustomerBindingUniqueID>FA8E995F-50D2E911-9475-0050560063F7</CustomerBindingUniqueID>
   <SoldToCustomerID>0000064397</SoldToCustomerID>
   <ReceivedFromCustomerID>QACN-CQ</ReceivedFromCustomerID>
   <TotalLineItems>2</TotalLineItems>
   <ProductBindings>
      <ProductBinding>
         <YZProductKeyID>3423553504516</YZProductKeyID>
         <XYSpecifics>
            <XYSerialNumber>5CD936C2FW</XYSerialNumber>
            <ConsumedDate>9/8/2019 3:47:37 PM</ConsumedDate>
         </XYSpecifics>
         <ServiceProductKeyID>4020124189204</ServiceProductKeyID>
      </ProductBinding>
      <ProductBinding>
         <YZProductKeyID>3423553504661</YZProductKeyID>
         <XYSpecifics>
            <XYSerialNumber>5CD936C1KK</XYSerialNumber>
            <ConsumedDate>9/8/2019 3:47:55 PM</ConsumedDate>
         </XYSpecifics>
         <ServiceProductKeyID>4020124189205</ServiceProductKeyID>
      </ProductBinding>
   </ProductBindings>
   <XYSpecifics>
      <SiteIdentifier>QACN-CQ</SiteIdentifier>
   </XYSpecifics>
</ProductBindingReportRequest>

We need to validate the xml input so based on the xml above I create the following xsd

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="ProductBindingReportRequest">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
        <xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
        <xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
        <xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
        <xsd:element name="ProductBindings">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element maxOccurs="unbounded" name="ProductBinding">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
                    <xsd:element name="XYSpecifics">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:element name="XYSerialNumber" type="xsd:string" />
                          <xsd:element name="ConsumedDate" type="xsd:string" />
                        </xsd:sequence>
                      </xsd:complexType>
                    </xsd:element>
                    <xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="XYSpecifics">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="SiteIdentifier" type="xsd:string" />
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

And we validate the xml with xsd by the method below

public static bool ValidateXML(string strXML, string strSchemePath, string strXMLSchemeName, ref string strException)
{
    if (strSchemePath.Substring(strSchemePath.Length - 1) != "\\")
        strSchemePath = strSchemePath + "\\";

    try
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add("http://schemas.ms.it.oem/digitaldistribution/2010/10", strSchemePath + strXMLSchemeName);
        settings.ValidationType = ValidationType.Schema;
        XmlReader readerCheck = XmlReader.Create(new StringReader(strXML), settings);
        XmlDocument checkXmlDocument = new XmlDocument();
        checkXmlDocument.Load(readerCheck);
        checkXmlDocument.RemoveAll();
        return true;
    }
    catch (Exception ex)
    {
        strException = ex.ToString();
        return false;
    }
}

Most of the received xmls work fine with the same implementation but the combination above gives me

The element 'XYSpecifics' in namespace 'http://schemas.ms.it.oem/digitaldistribution/2010/10' has invalid child element 'SiteIdentifier' in namespace 'http://schemas.ms.it.oem/digitaldistribution/2010/10'.

My questions are

  1. Is it since XYSpecifics exists both in and out side the ProductBindings?
  2. Is there any way I can adjust the xsd to make the validation works?
2 Answers

I am not entirely sure what

settings.Schemas.Add("http://schemas.ms.it.oem/digitaldistribution/2010/10", strSchemePath + strXMLSchemeName);

does to the XML reader instance, but you can also include this namespace in the XML schema document itself:

XML Schema

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.ms.it.oem/digitaldistribution/2010/10">
    <xsd:element name="ProductBindingReportRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
                <xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
                <xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
                <xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
                <xsd:element name="ProductBindings">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="ProductBinding">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
                                        <xsd:element name="XYSpecifics">
                                            <xsd:complexType>
                                                <xsd:sequence>
                                                    <xsd:element name="XYSerialNumber" type="xsd:string" />
                                                    <xsd:element name="ConsumedDate" type="xsd:string" />
                                                </xsd:sequence>
                                            </xsd:complexType>
                                        </xsd:element>
                                        <xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="XYSpecifics">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="SiteIdentifier" type="xsd:string" />
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

This should work, although it is a bit unfortunate that the element XYSpecifics is defined twice in this schema, with different child elements. A better way to write the schema would be to define this type once. For instance:

XML Schema (improved)

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://schemas.ms.it.oem/digitaldistribution/2010/10"
    xmlns="http://schemas.ms.it.oem/digitaldistribution/2010/10">
    <xsd:element name="ProductBindingReportRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="CustomerBindingUniqueID" type="xsd:string" />
                <xsd:element name="SoldToCustomerID" type="xsd:unsignedShort" />
                <xsd:element name="ReceivedFromCustomerID" type="xsd:string" />
                <xsd:element name="TotalLineItems" type="xsd:unsignedByte" />
                <xsd:element name="ProductBindings">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element maxOccurs="unbounded" name="ProductBinding">
                                <xsd:complexType>
                                    <xsd:sequence>
                                        <xsd:element name="YZProductKeyID" type="xsd:unsignedLong" />
                                        <xsd:element name="XYSpecifics" type="XYSpecificsType"/>
                                        <xsd:element name="ServiceProductKeyID" type="xsd:unsignedLong" />
                                    </xsd:sequence>
                                </xsd:complexType>
                            </xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
                <xsd:element name="XYSpecifics" type="XYSpecificsType"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    
    <xsd:complexType name="XYSpecificsType">
        <xsd:choice maxOccurs="3">
            <xsd:element name="XYSerialNumber" type="xsd:string" />
            <xsd:element name="ConsumedDate" type="xsd:string" />
            <xsd:element name="SiteIdentifier" type="xsd:string" />
        </xsd:choice>
    </xsd:complexType>
    
</xsd:schema>

You have written a schema that validates element that are in no namespace. You need to write a schema for elements in namespace "http://schemas.ms.it.oem/digitaldistribution/2010/10", which you can do by setting the targetNamespace attribute on the xs:schema element. This must be done in the schema itself, it can't be done in the validation API.

Related