gpt4 book ai didi

c# - 使用 XmlInclude 装饰的基类在序列化时仍会抛出类型未知异常是有原因的吗?

转载 作者:IT王子 更新时间:2023-10-29 03:55:40 55 4
gpt4 key购买 nike

我将简化代码以节省空间,但所呈现的内容确实说明了核心问题。

我有一个类,它有一个基类型的属性。有 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/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com