- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我将简化代码以节省空间,但所呈现的内容确实说明了核心问题。
我有一个类,它有一个基类型的属性。有 3 个派生类可以分配给该属性。
如果我将任何派生类分配给容器并尝试序列化容器,XmlSerializer 会抛出可怕的:
"The type x was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."
但是我的基类已经用那个属性修饰了,所以我认为必须有一个额外的“隐藏”要求。
真正奇怪的是,默认的 WCF 序列化程序对此类层次结构没有任何问题。
容器类
[DataContract]
[XmlRoot(ElementName = "TRANSACTION", Namespace = Constants.Namespace)]
public class PaymentSummaryRequest : CommandRequest
{
[DataMember]
public PaymentSummary Summary { get; set; }
public PaymentSummaryRequest()
{
Mechanism = CommandMechanism.PaymentSummary;
}
}
基类
[DataContract]
[XmlInclude(typeof(xPaymentSummary))]
[XmlInclude(typeof(yPaymentSummary))]
[XmlInclude(typeof(zPaymentSummary))]
[KnownType(typeof(xPaymentSummary))]
[KnownType(typeof(yPaymentSummary))]
[KnownType(typeof(zPaymentSummary))]
public abstract class PaymentSummary
{
}
派生类之一
[DataContract]
public class xPaymentSummary : PaymentSummary
{
}
序列化代码
var serializer = new XmlSerializer(typeof(PaymentSummaryRequest));
serializer.Serialize(Console.Out,new PaymentSummaryRequest{Summary = new xPaymentSummary{}});
异常
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type xPaymentSummary was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write13_PaymentSummary(String n, String ns, PaymentSummary o, Boolean isNullable, Boolean needType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write14_PaymentSummaryRequest(String n, String ns, PaymentSummaryRequest o, Boolean isNullable, Boolean needType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentSummaryRequest.Write15_TRANSACTION(Object o) --- 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 UserQuery.RunUserAuthoredQuery() in c:\Users\Tedford\AppData\Local\Temp\uqacncyo.0.cs:line 47
最佳答案
您看到的问题是因为 PaymentSummaryRequest
正在设置命名空间。您可以删除命名空间或将命名空间添加到 PaymentSummary
类:
[XmlRoot(Namespace = Constants.Namespace)]
[XmlInclude(typeof(xxxPaymentSummary))]
public abstract class PaymentSummary
{
}
正如@Tedford 在他下面的评论中提到的,只有在使用派生类型时才需要命名空间。
看起来在生成 XML 序列化程序集时,由于 Root 节点设置了命名空间而基类没有,所以它不在生成的序列化程序集中包含 XML Include 逻辑。
解决该问题的另一种方法是从类本身中删除命名空间声明,并在 XmlSerializer
构造函数上指定命名空间:
var serializer = new XmlSerializer(
typeof(PaymentSummaryRequest),
Constants.Namespace
);
关于c# - 使用 XmlInclude 装饰的基类在序列化时仍会抛出类型未知异常是有原因的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4616505/
我想创建一个像这样的结构: 根中的项目是公共(public)基类的后代。我就是无法让它工作。以下代码段创建 [Serializable] [XmlInc
我有一个类层次结构,我想使用 XmlSerializer 类及其相关属性对其进行序列化。有一个基本抽象类,然后是相当多的派生类(在我下面的代码中,我已经将派生类的数量减少到五个,但在实际代码中还有更多
我正在使用 .NET 序列化反序列化名为 Method 的类。 Method 包含实现 IAction 的对象列表。我最初使用 [XmlInclude]属性指定实现 IAction 的所有类。 但是现
我有以下类(class) [XmlRoot] public class AList { public List ListOfBs {get; set;} } public class B {
我将简化代码以节省空间,但所呈现的内容确实说明了核心问题。 我有一个类,它有一个基类型的属性。有 3 个派生类可以分配给该属性。 如果我将任何派生类分配给容器并尝试序列化容器,XmlSerialize
我有几个 WCF 数据契约(Contract)如下: [DataContract] public class Hatchback: Car {
我正在开发一个网络服务,它返回我在网络服务中定义的类数组。当我测试它时,我得到:“System.InvalidOperationException:类型 WebSite+HostHeader 不是预期
我在 Web 服务调用中使用抽象类作为参数。目前,我在基类中包含派生类的 XmlInclude,如下所示: [XmlInclude(typeof(DerivedClass))] public abst
我有这种情况: namespace MyApp.Animals { public class Dog : MyApp.Categories.Canine ... 所以我正在尝试序列化/反序列化
在使用 .NET 的 XmlSerializer 时,我遇到了一个非常奇怪的问题。 采用以下示例类: public class Order { public PaymentCollection
我有一个关于这个序列化问题的奇怪案例 - 这个问题已经在这个网站上被问过很多次了,我已经解决了其中的一些问题,并尝试了一些常用的方法但无济于事: 将 [XmlInclude] 添加到引发错误的类中 删
请考虑以下事项。您有一个要使用 XmlSerializer 序列化的类,该类具有带有类型约束的公共(public)泛型方法,其中类型位于不同的程序集中: using BarStuff; namespa
我调用的是别人的web服务,他们提供了一个WSDL文件和一堆XSD文件。我已经使用本地 WSDL 文件在我的项目中创建了 Web 引用,并使用 xsd.exe 创建了一个类。我调用的网络方法是 obj
因此,当我可以在 MSMQ 上放置消息时,旧代码库运行良好。但是,我收到的新代码库不起作用,老实说,我不知道我缺少什么。 基类: [DataContract] [KnownType(t
我是一名优秀的程序员,十分优秀!