2011-04-12 23 views
2

我想在我的定製配置部分中使用枚舉。所以我實現了一個枚舉DatabaseMode和相應的屬性。定製配置部分中的Enum IntellisSense

我還在我的System.Configuration.ConfigurationElement中實施了相應的Property。但是爲了使IntelliSense在web.config中工作,我需要提供以xsd格式鏡像相同結構的模式定義(xsd)。

我的問題是模式應該如何支持枚舉?

不同的選項枚舉:

public enum DatabaseMode 
{ 
    Development, 
    Deployment, 
    Production 
} 

屬性存儲有關模式的信息:

[ConfigurationProperty(databaseAlias)] 
public DatabaseElement Database 
{ 
    get { return (DatabaseElement)this[databaseAlias]; } 
    set { this[databaseAlias] = value; } 
} 

下面我schema文件的重要組成部分:

<xs:element name="database"> 
    <xs:complexType> 
    <xs:attribute name="server" type="xs:anyURI" use="required" /> 
    <xs:attribute name="name" type="xs:string" use="required" /> 
    <xs:attribute name="user" type="xs:string" use="required" /> 
    <xs:attribute name="password" type="xs:string" use="required" /> 
    </xs:complexType> 
</xs:element> 

回答

1

你實際上可以在XSD中定義一個枚舉。對於您的示例DatabaseMode屬性,XSD片段將如下所示:

<xs:attribute name="databaseMode"> <!-- I'm assuming you're using camelCasing --> 
    <xs:simpleType> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="development" /> 
      <xs:enumeration value="deployment" /> 
      <xs:enumeration value="production" /> 
     </xs:restriction> 
    </xs:simpleType> 
</xs:attribute> 

希望有所幫助。

如果有其他人想要回答的問題是,一旦創建了XSD,應該在什麼位置放置它,以便Visual Studio在web.config文件中識別它?

更新:我在SO上找到了我上面問題here的答案。