简体   繁体   中英

C# Validate simple XML against XSD does not work when using a prefix

I am trying to validate an XML file using XSD in C#:

C# code:

XmlReaderSettings booksSettings = new XmlReaderSettings();
XmlSchema xs = booksSettings.Schemas.Add("urn.mota:ndd.acu.gisae.VicuSolution111", "validation.xsd");
booksSettings.ValidationType = ValidationType.Schema;
booksSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
booksSettings.ValidationEventHandler += new ValidationEventHandler(valEventHandler);
XmlReader books = XmlReader.Create("data.xml", booksSettings);
while (books.Read()) {  }
Console.ReadLine();

data.xml:

<VicuSolution111 xmlns:ns0="urn.mota:ndd.acu.gisae.VicuSolution111">
  <ns0:Header>
      <ns0:TransactionType></ns0:TransactionType>
  </ns0:Header>
</VicuSolution111>

validation.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="urn.mota:ndd.acu.gisae.VicuSolution111"
           elementFormDefault="qualified">
  <xs:element name="VicuSolution111">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="1" name="Header">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="1" maxOccurs="1" name="TransactionType">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:minLength value="1"/>
                    <xs:maxLength value="40"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

When I execute the validation with ReportValidationWarnings I get the following messages:

Warning Could not find schema information for the element 'VicuSolution111'.
Warning Could not find schema information for the element 'urn.mota:ndd.acu.gisae.VicuSolution111:Header'.
Warning Could not find schema information for the element 'urn.mota:ndd.acu.gisae.VicuSolution111:TransactionType'.

But I am expecting to get an error due to the empty TransactionType node. If I modify the data.xml by adding xmlns="urn.mota:ndd.acu.gisae.VicuSolution111" without the ns0 prefix, the validations works.

<VicuSolution111 xmlns:ns0="urn.mota:ndd.acu.gisae.VicuSolution111"
                 xmlns="urn.mota:ndd.acu.gisae.VicuSolution111">
  <ns0:Header>
    <ns0:TransactionType></ns0:TransactionType>
  </ns0:Header>
</VicuSolution111>

This gives the expected message:

Error: The 'urn.mota:ndd.acu.gisae.VicuSolution111:TransactionType' element is invalid - The value '' is invalid according to its datatype 'String' - The actual length is less than the MinLength value.

Does anyone knows why this is happening and how I can validate the document without modifying the original XML file and keeping xmlns:ns0?

First off your schema has no targetnamespace, if you change that then it mostly works, ie

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2017 Liquid Studio - Data Designer Edition 15.0.0.6978 (https://www.liquid-technologies.com)-->
<xs:schema xmlns="urn.mota:ndd.acu.gisae.VicuSolution111" 
           elementFormDefault="qualified" 
           targetNamespace="urn.mota:ndd.acu.gisae.VicuSolution111" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="VicuSolution111">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Header" minOccurs="0" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="TransactionType" minOccurs="1" maxOccurs="1">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:minLength value="1" />
                                        <xs:maxLength value="40" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

But then your XML is wrong, if you modify it like this it would work

<?xml version="1.0" encoding="utf-8" ?>
<ns0:VicuSolution111  xmlns:ns0="urn.mota:ndd.acu.gisae.VicuSolution111">
  <ns0:Header>
    <ns0:TransactionType></ns0:TransactionType>
  </ns0:Header>
</ns0:VicuSolution111>

If your original XML was the output you wanted then you need to break your schema into 2 files, one containing VicuSolution111 with no targetnamespace the other containing Header with the targetnamespace set to 'urn.mota:ndd.acu.gisae.VicuSolution111'.

Incidently, the reason the parser could not find a schema definition for VicuSolution111 (which has no namespace in your xml file) is because you told the parser that the schema represents items in the namespace 'urn.mota:ndd.acu.gisae.VicuSolution111'.

    XmlSchema xs = booksSettings.Schemas.Add("urn.mota:ndd.acu.gisae.VicuSolution111", "validation.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