简体   繁体   English

XML 验证与 C#

[英]XML validation with C#

Good day folks.美好的一天,伙计们。 I am having some issues trying to get an XML file validated against it's schema: The XML file I have looks like this:我在尝试获取针对其架构验证的 XML 文件时遇到了一些问题:我拥有的 XML 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<game:gameXML xmlns:game="national:game" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header>
    <From description="Gamer">Gamer</From>
    <To description="Player">Player</To>
    <MessageId>1620711716</MessageId>
    <MessageDate>2021-05-11T15:41:53</MessageDate>
    <TransactionGroup>Ball</TransactionGroup>
    <Priority>Medium</Priority>
    <SecurityContext>CONTACT_PERSON_GOES_HERE</SecurityContext>
    <Market>Japan</Market>
  </Header>
  <Transactions>
    <Transaction transaction="1620711716">
      <ReportRequest>
        <ReportParameters xsi:type="game:reportParameters">
          <ReportName>All</ReportName>
          <FromDate>2005-01-01</FromDate>
          <ToDate>2021-05-11</ToDate>    
        </ReportParameters>
      </ReportRequest>
    </Transaction>
  </Transactions>
</game:gameXML>

I used VS 2019 to generate the corresponding schema, which gives me this:我使用 VS 2019 生成了相应的架构,这给了我这个:

xmlFile.xsd xmlFile.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:game="national:game" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="national:game" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:import schemaLocation="xmlContent.xsd" />
    <xs:element name="gameXML">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Header" />
                <xs:element ref="Transactions" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

and

xmlContent.xsd xmlContent.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Header">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="From">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="description" type="xs:string" use="required" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="To">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="description" type="xs:string" use="required" />
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="MessageId" type="xs:unsignedInt" />
                <xs:element name="MessageDate" type="xs:dateTime" />
                <xs:element name="TransactionGroup" type="xs:string" />
                <xs:element name="Priority" type="xs:string" />
                <xs:element name="SecurityContext" type="xs:string" />
                <xs:element name="Market" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Transactions">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Transaction">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="ReportRequest">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="ReportParameters">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="ReportName" type="xs:string" />
                                                    <xs:element name="FromDate" type="xs:date" />
                                                    <xs:element name="ToDate" type="xs:date" />
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="transaction" type="xs:unsignedInt" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The code I am using to load these is:我用来加载这些的代码是:

XmlReaderSettings settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Parse };
XmlSchemaSet xs = new XmlSchemaSet();
xs.Add("national:game", "xmlFile.xsd");
xs.Add("national:game", "xmlContent.xsd");
xs.Compile();

As soon as I run this it explodes with error:一旦我运行它,它就会出现错误:

System.Exception: This is an invalid xsi:type 'national:game:reportParameters'.

Clearly it is injecting the namespace into the xmlContent.xsd file and I can't figure out what needs to be set to prevent this.显然它将命名空间注入到xmlContent.xsd文件中,我不知道需要设置什么来防止这种情况。 In fact, this.xsd file should have it's own namespace (see <ReportParameters xsi:type="game:reportParameters"> in the actual.xml file).事实上,this.xsd 文件应该有它自己的命名空间(参见实际的.xml 文件中的<ReportParameters xsi:type="game:reportParameters"> )。

Any clues, pointers or help would be tremendously appreciated.任何线索、指示或帮助将不胜感激。 Thank you.谢谢你。

None of your types are named, so it looks as though there are no valid values for xsi:type besides the schema built-in types because all the complexTypes are anonymous.您的所有类型都没有命名,因此看起来xsi:type除了架构内置类型之外没有有效值,因为所有 complexTypes 都是匿名的。

This is one respect where xsd.exe (the tool used to generate schemas) is limited, since by definition if an xsi:type attribute is present it must refer to some type declared by the schema, but the generated schema declares no named types.这是 xsd.exe(用于生成模式的工具)受到限制的一个方面,因为根据定义,如果存在 xsi:type 属性,它必须引用模式声明的某些类型,但生成的模式没有声明命名类型。 However its purpose is to resolve ambiguity, so it wouldn't be logical for that same tool to introduce ambiguity.然而,它的目的是解决歧义,因此同样的工具引入歧义是不合逻辑的。

To fix this, restructure xmlContent.xsd to declare the reportParameters type globally and reference it ie要解决此问题,请重构xmlContent.xsd以全局声明reportParameters类型并引用它,即

<xs:element name="ReportParameters" type="game:reportParameters"/>

<xs:complexType name="reportParameters">
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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