Cannot generate classes because no top-level elements with complex type where found

白昼怎懂夜的黑 提交于 2019-11-30 20:02:58

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.

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>

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.

The code above is importable with xsd.exe without any problems.

It would be better to use SvcUtil.exe instead of xsd.exe. It will allow you to avoid the problem with generating classes based on types-only XSDs. An example of .bat file:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools\SvcUtil.exe MySchema.xsd TypesSchema.xsd /dconly /n:*,MyNamespace
pause
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!