簡體   English   中英

C#使用前綴驗證針對XSD的簡單XML無效

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

我正在嘗試在C#中使用XSD驗證XML文件:

C#代碼:

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>

當我使用ReportValidationWarnings執行驗證時,收到以下消息:

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'.

但由於空的TransactionType節點,我預計會收到錯誤。 如果我通過添加不帶ns0前綴的xmlns="urn.mota:ndd.acu.gisae.VicuSolution111"修改data.xml,則驗證有效。

<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>

這給出了預期的消息:

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.

有誰知道為什么會這樣,以及如何在不修改原始XML文件和保留xmlns:ns0的情況下驗證文檔?

首先,您的架構沒有targetnamespace,如果您更改了它,那么它通常會起作用,即

<?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>

但是,那么您的XML是錯誤的,如果您這樣修改它,它將可以工作

<?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>

如果原始XML是所需的輸出,則需要將模式分為2個文件,一個文件包含沒有目標名稱空間的VicuSolution111,另一個文件包含目標名稱空間設置為“ urn.mota:ndd.acu.gisae.VicuSolution111”的標頭。

順便說一句,解析器找不到VicuSolution111(在xml文件中沒有名稱空間)的架構定義的原因是因為您告訴解析器該架構代表名稱空間'urn.mota:ndd.acu.gisae.VicuSolution111中的項目'。

    XmlSchema xs = booksSettings.Schemas.Add("urn.mota:ndd.acu.gisae.VicuSolution111", "validation.xsd");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM