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
- Is it since
XYSpecificsexists both in and out side theProductBindings? - Is there any way I can adjust the xsd to make the validation works?