代码之家  ›  专栏  ›  技术社区  ›  Jon

xsd.exe/dataset未从我的xsd文件创建枚举

  •  2
  • Jon  · 技术社区  · 15 年前

    我创建了一个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枚举属性吗? This link says that it should . 也许我做错了什么?不,在.cs文件中的何处可以看到枚举。

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

    谢谢。

    更新:

    似乎我是在使用xsd.exe创建数据集(/d switch),当时我应该创建一个类(/c switch)。在我设置它生成一个类之后,它就正常工作了。

    3 回复  |  直到 9 年前
        1
  •  2
  •   marc_s    15 年前

    我不知道在你的情况下会发生什么-我把你的代码复制到 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,
    }
    

    我肯定有两个枚举 CategoryType MarketType .

    我所做的就是把你的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的哪个版本?

    马克

        2
  •  1
  •   Cheeso    15 年前

    对我有用吗?但我必须添加一个模式元素,并在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,
    }
    
        3
  •  0
  •   alrts    9 年前

    如果定义 单纯形 未被架构中的任何字段使用,其 枚举 不会在生成的.cs文件中表示。

    同样,如果它被使用,但是在 联盟 与类型 字符串: 它的 枚举 仍然没有在生成的.cs文件中表示。

    但是,如果架构包含一个字段, 单纯形 原样(即不在 联盟 对于其他类型),它将在output.cs文件中表示。