2009-01-17 86 views
2

我使用IIS/asmx來支持Flash客戶端。我的一些服務層數據傳輸對象的屬性是枚舉值。有些情況下這些屬性應該爲空。C#枚舉屬性null與0

當與這樣的枚舉屬性空值的對象被呈現給皁,我收到此錯誤:

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: Instance validation error: '0' is not a valid value for NameSpaceX.Model.NodeType. 
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_NodeType(NodeType v) 
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_PackageDTO(String n, String ns, PackageDTO o, Boolean isNullable, Boolean needType) 
    at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write15_ArrayOfPackageDTO(Object o) 
    at Microsoft.Xml.Serialization.GeneratedAssembly.ListOfPackageDTOSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer) 
    at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) 
    --- End of inner exception stack trace --- 
    at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) 
    at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces) 
    at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o) 
    at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue) 
    at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) 
    at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) 
    at System.Web.Services.Protocols.WebServiceHandler.Invoke() 

有更好的溶液,然後以創建具有0值的枚舉構件(例如[ 0,「空」]我懷疑我缺乏基本的知識在這裏。

感謝。

回答

4

我的第一個傾向是您指向Nullable<T>類型。但是,四處尋找文檔上Nullable<T>一後nd SOAP,看起來它可能不被支持。你可能想進一步探索。

這就是說,Enums聲明一個NotSet = 0成員是很常見的。