2009-08-17 44 views
2

我已經創建了一個XSD並且已經在該.xsd文件之上運行了XSD.exe。看來,我的簡單類型被限制爲枚舉值,並不是在輸出的.cs文件中生成爲枚舉。XSD.exe/dataset沒有從我的xsd文件創建枚舉

例如,我的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文件中的任何位置可以看到枚舉。

如果您需要更多信息,請告訴我可以提供什麼。

謝謝。

UPDATE:

看來,我是用XSD.EXE創建數據集(/ d開關),當我應該已經創建一個類(/ c開關)。在我設置它生成一個類後,它工作正常。

回答

2

我不知道你的情況會發生什麼 - 我複製你的代碼到enum.xsdxsd.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?

馬克

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, 
} 
+0

你的例子幫助我在它的情況下工作。之前,我有一個生成的字符串類型。在我添加一個'xmlns:tns =「mynamespace」'並且在屬性聲明中使用了'tns:'後,我產生了一個枚舉類型。 – 2015-05-07 19:05:06

0

如果定義的simpleType未在架構中使用的任何領域,其枚舉將不會在所得的cs文件表示。

同樣地,如果它是用來但在一個工會與類型的xs:串,其枚舉仍然沒有在所得的cs文件表示。

然而,如果該模式包含一個字段,其原樣是(即,不在聯合與其他類型),它將在輸出.cs文件中表示。