简体   繁体   中英

The element '...' in namespace '...' has invalid child element, even though child element exists in schema

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}");
    }
}

In your XSD, change

elementFormDefault="unqualified"

to

elementFormDefault="qualified"

Your explicit setting of elementFormDefault="unqualified" (which also happens to be the default setting) means that locally declared elements are in no namespace. This is undesired in your case (and rarely ever desired, in fact). For further details, see What does elementFormDefault do in XSD?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM