2009-08-17 36 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文件中表示。