7

For some reason, MS .Net (v3.5) tool - xsd.exe doesn't generate types when they are not used inside any element.

e.g.

XSD File (I threw in the complex element to avoid this warning - "Warning: cannot generate classes because no top-level elements with complex type were found."):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:simpleType name="EnumTest">
    <xs:restriction base="xs:string">
      <xs:enumeration value="item1" />
      <xs:enumeration value="item2" />
      <xs:enumeration value="item3" />
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="myComplexType">
    <xs:attribute name="Name" use="required" type="xs:string"/>
  </xs:complexType>
  <xs:element name="myElem" type="myComplexType"></xs:element>
</xs:schema>

When i run this thru xsd.exe using

xsd /c xsdfile.xsd

I don't see EnumTest in the generated cs file.

Note; Even though I don't use the enum here, but in my actual project, I have cases like this where we send enum's string value as output.

How can I force the xsd tool to include these? Or should I switch to some other tool?

I work in Visual Studio 2008.

4
  • Your simpletype isn't used by any elements... Use it, then try again. Jan 26, 2010 at 20:09
  • i guess u haven't read my post completely!
    – Mrchief
    Jan 26, 2010 at 20:43
  • 1
    I did read your post. But if there's no element using the type, then the system won't define a type. Add <xs:element name="Something" type="EnumTest"></xs:element> and then see what it does. (It's an optimization thingie...) Jan 27, 2010 at 7:36
  • 1
    that is exactly my problem. why does the tool assume that I won't need it if there is no element? what's the point of calling it a 'type' when the actual 'element' is the type? no offense to you but seems to me that now-a-days anything goes in the name of optimization!
    – Mrchief
    Jan 27, 2010 at 18:21

5 Answers 5

4

I'll have to conclude this is a stupid shortcoming of the tool. Maybe give a switch to turn on this behavior. By not having this behavior, I'm forced to create types outside of xsd and that creates fragmented code.

This is my personal opinion and I'm pretty sure there are others who would share the same.

6
  • The tool generates classes for the schema. If the schema were to include, say, the XAML namespace, then without the optimisation indicated by Wim ten Brink it would need to create classes for almost the entire world of XML. This is generally undesired. If the elements are not referenced in the schema, then they are elided.
    – Pekka
    Nov 13, 2013 at 9:11
  • That's an edge case and I'm not saying do it for every import. Just for my schema. But again, you can have switches to control granularity of it.
    – Mrchief
    Nov 13, 2013 at 16:32
  • I would have thought that the eXtensible in XML strongly suggests that most schemata would import others, but that most software would remain interested in only its local types, leaving the rest of the data as uninterpreted payload. There may also be a misunderstanding. Elements are the public interface, whereas types are notational convenience. You can always define the elements with anonymous types if you only want one kind of named artifact in the schema.
    – Pekka
    Nov 13, 2013 at 20:29
  • 1
    Tools are intended to support the specification they implement. In W3C XML Schema Definition Language (XSD) 1.1 Part 1: Structures 2.2.2.1 Element Declaration, "An element declaration is an association of a name with a type definition, either simple or complex, an (optional) default value and a (possibly empty) set of identity-constraint definitions." A type is not a complete definition and is intended to support elements.
    – Pekka
    Nov 16, 2013 at 19:03
  • 1
    Are you part of the team that authored this tool? I'm not sure what your point is. Why is the type not a complete definition? Read your own quote - elements are just an association to a definition. Sorry to say, but seems that you're just arguing for the sake of argument.
    – Mrchief
    Nov 18, 2013 at 16:15
4

I know this is very old, but it came up in google when I was searching and I found an answer.

An Xsd has to have at lease one xs:element to be valid and for xsd.exe to work right.

look at this for more info http://keithelder.net/2008/11/02/creating-a-rest-wcf-service-from-an-existing-xsd-schema/

2

It is possible to create enums within the XSD file even if you do not intend to use them for validation. Add the following to the XSD:

<xs:element name="DummyEnumTest" type="EnumTest" abstract="true" block="#all"/>

(where EnumTest is the enum you want to see generated).

The abstract attribute ensures that the element cannot play the role of the document element of an instance document. The block attribute is less important.

There are other ways of accomplishing the same goal like declaring a prohibited attribute with your enumerated type anywhere you like. I find it useful to encapsulate all my unused enums within a generic wrapper like this, to minimize global declarations:

<xs:element name="ForceGenerationOfBaseEnums" abstract="true" block="#all">
    <xs:complexType>
        <xs:choice>
            <xs:element name="..." type="..."/>
            <xs:element name="..." type="..." />
        </xs:choice>
    </xs:complexType>
</xs:element>
2
  • Which again brings me to my initial gripe - it forces you you to declare at least one unused element for the generation to happen.
    – Mrchief
    Apr 19, 2013 at 14:38
  • @Mrchief - xsd.exe is a thin wrapper around .NET classes. There is nothing magic around it. Download the source of Xsd2Code, which is an open source wrapper of a similar kind (much more flexible than xsd.exe anyway) and use or adapt it to your needs if you cannot accept cosmetic imperfections of xsd.exe. Look also at this question. Apr 21, 2013 at 19:43
1

I came across a similar problem, with xs:complexType not being inside an xs:element, and thus not being included in the generated cs file. In our scenario we have a wsdl file that imports two xsd files, so this might not apply to you.

Instead of running xsd.exe on those two xsd files, we did the following:

wsdl.exe /language:CS /out:OutputDir OurService.wsdl first.xsd second.xsd

That worked like a charm, and generated everything, including the complex types.

1
  • Yeah, that was another option at that time. Even WCF's generator handles this correctly though not sure. I've been away from xsd for a long time now so can't speak for sure.
    – Mrchief
    Feb 29, 2012 at 3:54
0

If you don't use the enum here, or in any other class you're generating through the xsd tool, then define it in your project somewhere else just as you would any other enum. If you absolutely need to have the xsd tool create a class for you, then Workshop Alex's solution is the most commonly used workaround in this case (I don't even really consider it a workaround, its actually very convenient to be able to utilize the tool in this way)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.