classxsdauto-generatexsd.exexsd2code

XSD Cannot generate classes but works on similar file


I converted a lot of xsd to C# in the past, but today I'm facing a new error message, for me: "cannot generate classes because no top-level elements with complex type were found."

I have this problem on 2 files. I read a lot of posts about this, and they helped me to solve at least 1 of the 2 problems I have.

The file I fixed was:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:mg="urn:crif-messagegateway:2006-08-23" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:crif-messagegateway:2006-08-23" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="MGRequest" type="xs:string"/>
    <xs:element name="MGResponse" type="xs:string"/>
</xs:schema>

and I edited it to:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:mg="urn:crif-messagegateway:2006-08-23" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:crif-messagegateway:2006-08-23" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="MGRequest">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:string" />
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
    <xs:element name="MGResponse">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:string" />
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
</xs:schema>

Now, I'm trying also to convert the following xsd file (quite similar to the previous fixed), but it xsd.exe throws the error "cannot generate classes because no top-level elements with complex type were found.". Which is the problem? What are the differences between the working file above?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:mg="urn:crif-messagegateway:2006-08-23" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:crif-messagegateway:2006-08-23" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="MGRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:any namespace="##other"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="MGResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:any namespace="##other"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

I tried also with xsd2code, but what I get is just an empty class.


Solution

  • I guess it doesn't see the need to create a class to wrap a primitive type, if you run it through Liquid XML Objects you get this

    namespace LiquidTechnologies.GeneratedLx.Mg
    {
        #region Elements
        /// <summary>A class representing the root XSD element MGRequest@urn:crif-messagegateway:2006-08-23</summary>
        /// <XsdPath>schema:schema.xsd/element:MGRequest</XsdPath>
        /// <XsdFile>file://sandbox/schema.xsd</XsdFile>
        /// <XsdLocation>3:5-3:52</XsdLocation>
        [LxSimpleElementDefinition("MGRequest", "urn:crif-messagegateway:2006-08-23", ElementScopeType.GlobalElement)]
        public partial class MGRequestElm
        {
            /// <summary>Holds the <see cref="System.String" /> (xs:http://www.w3.org/2001/XMLSchema:string) value of the element</summary>
            /// <XsdPath>schema:schema.xsd/element:MGRequest</XsdPath>
            /// <XsdFile>file://sandbox/schema.xsd</XsdFile>
            /// <XsdLocation>3:5-3:52</XsdLocation>
            [LxValue(LxValueType.Value, XsdType.XsdString)]
            public System.String Value { get; set; } = "";
    
        }
    
        /// <summary>A class representing the root XSD element MGResponse@urn:crif-messagegateway:2006-08-23</summary>
        /// <XsdPath>schema:schema.xsd/element:MGResponse</XsdPath>
        /// <XsdFile>file://sandbox/schema.xsd</XsdFile>
        /// <XsdLocation>4:5-4:53</XsdLocation>
        [LxSimpleElementDefinition("MGResponse", "urn:crif-messagegateway:2006-08-23", ElementScopeType.GlobalElement)]
        public partial class MGResponseElm
        {
            /// <summary>Holds the <see cref="System.String" /> (xs:http://www.w3.org/2001/XMLSchema:string) value of the element</summary>
            /// <XsdPath>schema:schema.xsd/element:MGResponse</XsdPath>
            /// <XsdFile>file://sandbox/schema.xsd</XsdFile>
            /// <XsdLocation>4:5-4:53</XsdLocation>
            [LxValue(LxValueType.Value, XsdType.XsdString)]
            public System.String Value { get; set; } = "";
    
        }
    
        #endregion
    
    }
    

    Liquid XML Objects is free for small xsd's.