I have an xml schema file that describes an element called MessageProcessingResult that can have child elements 'MessageID' and 'Category'. My xml clearly has these elements yet when I validate the xml against the schema I get an error stating the 'Category' element is invalid:
The element 'MessageProcessingResult' in namespace 'http://test.com/MessageProcessing' has invalid child element 'Category' in namespace 'http://test.com/MessageProcessing'. List of possible elements expected: 'Category, MessageID'.
I must be defining my schema incorrectly, but I don't know exactly what it is.
My schema, xml and validation code are as follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://test.com/MessageProcessing"
targetNamespace="http://test.com/MessageProcessing"
elementFormDefault="unqualified"
attributeFormDefault="unqualified">
<xs:element name="MessageProcessingResult">
<xs:complexType>
<xs:all>
<xs:element name="MessageID" type="xs:string" minOccurs="1" maxOccurs="1"/>
<xs:element name="Category">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Success"/>
<xs:enumeration value="Problem"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
xml that looks like this:
<MessageProcessingResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://test.com/MessageProcessing">
<Category>Success</Category>
<MessageID>id</MessageID>
</MessageProcessingResult>
I validate with this code:
public class XmlValidator
{
public void Validate(Stream strXml)
{
XmlReaderSettings settings = new XmlReaderSettings();
//settings.Schemas.Add(null, @"Schema\MessageProcessingResults.xsd");
settings.Schemas.Add(null, @"Schema\MessageProcessingResult.xsd");
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(XmlSchemaValidationEventHandler);
XmlReader xml = XmlReader.Create(strXml, settings);
this.Errors = new List<string>();
while (xml.Read()) { }
}
public List<string> Errors { get; set; }
private void XmlSchemaValidationEventHandler(object sender, ValidationEventArgs e)
{
this.Errors.Add($"{e.Severity} {e.Message}");
}
}