问题描述
我正在尝试使用 XSD.EXE 工具从 xsd 文件创建一个类.但由于某种原因,我收到此错误.
I am trying to create a class from a xsd file using the XSD.EXE tool. but for some reason i get this error.
警告:无法生成类,因为没有顶级元素发现复杂类型.
我在堆栈上环顾四周,看到我可以在复杂类型元素上放置一个类型,但我似乎无法让我工作.这是xsd文件
I have looked around on stack and seen that i could put a type on the complex type element but i cannot seem to get i to work. here is the xsd file
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="DocumentIdentification">
<xs:sequence>
<xs:element name="Standard" type="xs:string"/>
<xs:element name="TypeVersion" type="xs:string"/>
<xs:element name="InstanceIdentifier" type="xs:string"/>
<xs:element name="Type" type="xs:string"/>
<xs:element name="MultipleType" type="xs:boolean" minOccurs="0"/>
<xs:element name="CreationDateAndTime" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
谢谢!
推荐答案
你的 XSD 只定义了一个类型(正如 Sergio 所建议的那样).因此,它不能用于 XML 验证,除非它是由另一个 XSD 导入的.同样,像 xsd.exe
这样的其他工具将无法使用它.
You XSD only defines a type (as Sergio also suggested). As such, it cannot be used for validation of XML, unless it is imported by another XSD . Likewise, other tools like xsd.exe
will not be able to anything sensible with it.
您可以将其与具有接口定义但没有接口实现的 C# 库进行比较.
You can compare this with a C# library having an interface definition, but no implementation of the interface.
您可以通过多种方式解决此问题.考虑到您当前的代码,我会建议以下内容:
You can fix this in a variety of ways. Considering your current code, I would suggest something along those lines:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="DocumentIdentification">
<xs:sequence>
<xs:element name="Standard" type="xs:string" />
<xs:element name="TypeVersion" type="xs:string" />
<xs:element name="InstanceIdentifier" type="xs:string" />
<xs:element name="Type" type="xs:string" />
<xs:element name="MultipleType" type="xs:boolean" minOccurs="0" />
<xs:element name="CreationDateAndTime" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
<xs:element name="DocumentIdentification" type="DocumentIdentification" />
</xs:schema>
尽管您可以考虑重命名类型名称,以防止读者混淆.一种常见的模式是使用 Type
为类型名添加后缀,在您的情况下为 DocumentIdentificationType
.
Though you may consider renaming the type name, to prevent confusion to readers. A common pattern is to suffix typenames with Type
, in your case, DocumentIdentificationType
.
上面的代码可以用 xsd.exe
导入,没有任何问题.
The code above is importable with xsd.exe
without any problems.
这篇关于无法生成类,因为找不到具有复杂类型的顶级元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!