.Net xsd.exe tool does not generate all types

For some reason, the MS.Net tool (v3.5) - xsd.exe does not generate types if they are not used inside any element.

eg.

XSD file (I selected a complex element to avoid this warning - β€œWarning: classes cannot be created because top-level elements with a complex type were not 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 through xsd.exe using

xsd / c xsdfile.xsd

I do not see EnumTest in the generated cs file.

Note; Despite the fact that I do not use the enumeration here, but in my actual project, I have such cases when we send the value of the enumeration string as output.

How to make the xsd tool enable them? Or do I need to switch to another tool?

I work in Visual Studio 2008.

+7
visual-studio
source share
5 answers

I need to conclude that this is a silly tool flaw. Perhaps enable this behavior. Without this behavior, I am forced to create types outside of xsd and it creates fragmented code.

This is my personal opinion, and I am sure that there are others who will share the same.

+5
source share

I know this is very old, but it appeared on google when I searched and I found the answer.

Xsd must rent one xs: element, which must be valid, and for xsd.exe to work correctly.

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

+3
source share

You can create enumerations in an XSD file even if you are not going to use them for verification. Add the following to your 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 act as the document element of the document instance. The block attribute is less important.

There are other ways to achieve the same goal as declaring a forbidden attribute with your enumerated type anywhere. I find it useful to encapsulate all my unused enums in a common 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
source share

I ran into a similar problem, while xs:complexType not inside xs:element and therefore was not included in the generated cs file. In our scenario, we have a wsdl file that imports two xsd files, so this may not apply to you.

Instead of running xsd.exe in these two xsd files, we did the following:

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

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

+1
source share

If you are not using the enumeration here or in any other class that you generate using the xsd tool, then define it in your project somewhere else, like any other enumeration. If you absolutely need the xsd tool to create a class for you, then Workshop Alex is the most commonly used workaround in this case (I don’t even consider it a workaround, it’s actually very convenient to use the tool in this way)

0
source share

All Articles