c# - Web 服务序列化复杂对象

标签 c# web-services serialization web known-types

我有一个 Class1 的 Web 服务。 Class1 有一个属性的图像列表

[Serializable]
[SoapInclude(typeof(Bitmap))]

public class Class1
{
    private static List<Image> _myList = new List<Image>();

    public List<Image> MyList
    {
        get { return _myList; }

        set
        {
            _myList = value;
        }
    }
}

MyList上进行获取时,发生以下错误。有什么想法吗?

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Drawing.Bitmap was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_Image(String n, String ns, Image o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_Class1(String n, String ns, Class1 o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_HelloWorldResponse(Object[] p) at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.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(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle)
at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) at System.Web.Services.Protocols.WebServiceHandler.Invoke() --- End of inner exception stack trace ---

最佳答案

我在以下网址找到了答案。我需要 [XmlInclude] 标签而不是 [SoapInclude] 标签

Troubleshooting Common Problems with the XmlSerializer

    [Serializable]
    [XmlInclude(typeof(Bitmap))]
    public class Class1
    {

}

关于c# - Web 服务序列化复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10321456/

相关文章:

java - Java中的部分反序列化和序列化?

C# 将 DataGridView 保存到文本文件

c# - 我可以让 2 个不同的 Entity Framework 上下文共享一个 Sql Server Compact 数据库吗?

web-services - 我如何测试网络服务?

Java SOAP 服务和 JBOSS

android - 使用可序列化发送对象数组列表

c# 将选定的列表框项目复制到字符串中

c# - 针对 "%"搜索字符串

web-services - 用于 WSDL 和 BasicHttpBinding 的 F# 类型提供程序

java - 是否可以保存 Java Class<?> 文件?