简体   繁体   English

XSD.exe / dataset不能从我的xsd文件创建枚举

[英]XSD.exe /dataset is not creating enumerations from my xsd file

I have created an XSD and have run XSD.exe on top of that .xsd file. 我创建了一个XSD,并已在该.xsd文件上运行XSD.exe。 It seems that my simple types that are restricted to enumeration values are not being generated as enums in the outputted .cs file. 似乎我的限于枚举值的简单类型并未在输出的.cs文件中作为枚举生成。

For example, my xsd looks like this: 例如,我的xsd看起来像这样:

<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? 当我运行XSD.exe时,输出的.cs文件不应该为每个简单类型都具有xml枚举属性吗? This link says that it should . 这个链接说应该 Maybe I am doing something wrong? 也许我做错了什么? No where in my .cs file do I see an enum. 在.cs文件中的任何位置都看不到枚举。

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). 似乎我应该使用XSD.exe创建数据集(/ d开关),而应该创建类(/ c开关)。 After I set it generate a class, it worked correctly. 在我设置它生成一个类之后,它可以正常工作。

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: 我不知道您的情况如何-我将您的代码复制到enum.xsd并在xsd.exe上运行xsd.exe结果如下:

//------------------------------------------------------------------------------
// <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 . 我肯定有两个枚举CategoryTypeMarketType

All I did was put your XSD code into a <xsl:schema> tag: 我所做的只是将您的XSD代码放入<xsl:schema>标记中:

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

and then I ran XSD.EXE on it: 然后在上面运行XSD.EXE:

xsd.exe  enum.xsd  /c

which created the enum.cs file shown above. 它创建了enum.cs显示的enum.cs文件。

What version of XSD.EXE do you have? 您拥有什么版本的XSD.EXE? What version of .NET are you using? 您正在使用什么版本的.NET?

Marc

it works for me? 这个对我有用? But I had to add in a schema element, and insert an element inside ItemType. 但是我必须添加一个架构元素,然后在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,
}

If a defined simpleType is not used by any field in the schema, its enum will not be represented in the resulting .cs file. 如果架构中的任何字段未使用已定义的simpleType ,则其枚举将不会在结果.cs文件中表示。

Likewise, if it IS used but in a union with the type xs:string , its enum is still not represented in the resulting .cs file. 同样地,如果使用,但与XS工会 :字符串 ,它的枚举仍然没有在生成cs文件表示。

If, however, the schema contains a field with the simpleType as is (ie not in a union with some other type), it will be represented in the output .cs file. 然而,如果模式包含与所述的simpleType一个字段是(即,在不与一些其它类型的联合 ),它将在输出cs文件来表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM