2

我创建了一个 XSD 并在该 .xsd 文件之上运行 XSD.exe。似乎我的仅限于枚举值的简单类型没有在输出的 .cs 文件中生成为枚举。

例如,我的 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>

当我运行 XSD.exe 时,输出的 .cs 文件不应该对我的每个简单类型都有一个 xml 枚举属性吗? 这个链接说它应该。也许我做错了什么?在我的 .cs 文件中没有看到枚举。

如果您需要更多信息,请告诉我我能提供什么。

谢谢。

更新:

似乎我正在使用 XSD.exe 创建数据集(/d 开关),而我应该创建一个类(/c 开关)。在我设置它生成一个类后,它工作正常。

4

3 回答 3

2

我不知道您的情况会发生什么-我将您的代码复制到enum.xsd其中并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,
}

我肯定得到了两个枚举CategoryTypeMarketType.

我所做的只是将您的 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> 

然后我在上面运行 XSD.EXE:

xsd.exe  enum.xsd  /c

它创建了enum.cs上面显示的文件。

你有什么版本的 XSD.EXE?您使用的是哪个版本的 .NET?

马克

于 2009-08-17T20:25:48.257 回答
2

如果架构中的任何字段都未使用定义的simpleType ,则其枚举将不会在生成的 .cs 文件中表示。

同样,如果它被使用但与类型xs:string联合使用,则它的枚举仍然不会在生成的 .cs 文件中表示。

但是,如果模式包含一个按原样具有simpleType的字段(即不与其他类型联合),它将在输出 .cs 文件中表示。

于 2015-09-11T14:27:11.287 回答
1

这个对我有用?但是我必须添加一个模式元素,并在 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>

它生成这个(部分)

/// <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,
}
于 2009-08-17T20:08:17.650 回答