views:

748

answers:

2

I have created an XSD and have run XSD.exe on top of that .xsd file. It seems that my simple types that are restricted to enumeration values are not being generated as enums in the outputted .cs file.

For example, my xsd looks like this:

<xs:element name="ItemList" nillable="false">
 <xs:complexType>
  <xs:sequence minOccurs="1" maxOccurs="1">
   <xs:element name="Item" type="ItemType" minOccurs="1" maxOccurs="unbounded" nillable="false">
   </xs:element>
  </xs:sequence>
 </xs:complexType>
</xs:element>
<xs:complexType name="ItemType">
    <xs:sequence maxOccurs="1" minOccurs="1">
        <!-- other complex types, etc... -->
    </xs:sequence>
 <xs:attribute name="Market" type="MarketType" use="required">
 </xs:attribute>
 <xs:attribute name="Category" type="CategoryType" use="required" />
</xs:complexType>
<xs:simpleType name="CategoryType">
 <xs:restriction base="xs:string">
  <xs:enumeration value="Mild" />
  <xs:enumeration value="Hot" />
 </xs:restriction>
</xs:simpleType>
<xs:simpleType name="MarketType">
 <xs:restriction base="xs:string">
  <xs:enumeration value="Weak" />
  <xs:enumeration value="Strong" />
 </xs:restriction>
</xs:simpleType>

When I run XSD.exe shouldn't the outputted .cs file have an xml enum attribute for each of my simple types? This link says that it should. Maybe I am doing something wrong? No where in my .cs file do I see an enum.

If you need any more information, let me know what I can provide.

Thanks.

UPDATE:

It seems that I was using XSD.exe to create a dataset (/d switch), when I should have been creating a class (/c switch). After I set it generate a class, it worked correctly.

A: 

it works for me? But I had to add in a schema element, and insert an element inside ItemType.

<xs:schema
   elementFormDefault    ="qualified"
   targetNamespace       ="urn:Jon.Stackoverflow._2009aug.Example"
   xmlns:tns             ="urn:Jon.Stackoverflow._2009aug.Example"
   xmlns:xs              ="http://www.w3.org/2001/XMLSchema"
   >


<xs:element name="ItemList" nillable="false">
  <xs:complexType>
    <xs:sequence minOccurs="1"
                 maxOccurs="1">
      <xs:element name="Item"
                  type="tns:ItemType"
                  minOccurs="1"
                  maxOccurs="unbounded"
                  nillable="false">
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:complexType name="ItemType">
  <xs:sequence maxOccurs="1" minOccurs="1"> 
     <xs:element type="xs:int" name="num"/>
  </xs:sequence>
  <xs:attribute name="Market"
                type="tns:MarketType"
                use="required">
  </xs:attribute>
  <xs:attribute name="Category" type="tns:CategoryType" use="required" />
</xs:complexType>

<xs:simpleType name="CategoryType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Mild" />
    <xs:enumeration value="Hot" />
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="MarketType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Weak" />
    <xs:enumeration value="Strong" />
  </xs:restriction>
</xs:simpleType>

</xs:schema>

It generates this (in part)

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:Jon.Stackoverflow._2009aug.Example")]
public enum MarketType {

    /// <remarks/>
    Weak,

    /// <remarks/>
    Strong,
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:Jon.Stackoverflow._2009aug.Example")]
public enum CategoryType {

    /// <remarks/>
    Mild,

    /// <remarks/>
    Hot,
}
Cheeso
+1  A: 

I don't know what happens in your case - I copied your code into enum.xsd and ran xsd.exe on it - here's the result:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4016
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class ItemList {

    private ItemType[] itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public ItemType[] Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ItemType {

    private MarketType marketField;

    private CategoryType categoryField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public MarketType Market {
        get {
            return this.marketField;
        }
        set {
            this.marketField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public CategoryType Category {
        get {
            return this.categoryField;
        }
        set {
            this.categoryField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
public enum MarketType {

    /// <remarks/>
    Weak,

    /// <remarks/>
    Strong,
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
public enum CategoryType {

    /// <remarks/>
    Mild,

    /// <remarks/>
    Hot,
}

I definitely got the two enums CategoryType and MarketType.

All I did was put your XSD code into a <xsl:schema> tag:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TheParentNode" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  ..... (inserted your code here) .......
</xs:schema>

and then I ran XSD.EXE on it:

xsd.exe  enum.xsd  /c

which created the enum.cs file shown above.

What version of XSD.EXE do you have? What version of .NET are you using?

Marc

marc_s