具有动态内容的 XmlSerializer、XmlArray...怎么样?

发布于 2024-10-30 22:40:25 字数 758 浏览 4 评论 0原文

首先:这也适用于 REST 反序列化,因此自定义 XmlSerializer 是不可能的。

我有一个类的层次结构,需要从“信封”进行序列化和反序列化。它有一个名为“Items”的数组元素,可以包含抽象“Item”的子类。

[XmlArray("Items")]
public Item [] Items { get; set; }

现在我需要添加 XmlArrayItem,但数量不是“固定”的。到目前为止,我们使用反射来查找具有 KnownTypeProvider 的所有子类,因此可以轻松地使用新的子类型扩展程序集。我真的不想在这里对所有项目进行硬编码。

该类是相应定义的:

[XmlRoot]
[KnownType("GetKnownTypes")]
public class Envelope {

但这没有帮助。

将项目更改为:

[XmlArray("Items")]
[XmlArrayItem(typeof(Item))]
public Item [] Items { get; set; }

结果:

{“类型 xxx.调整 没想到。使用 XmlIninclude 或 SoapIninclude 属性来指定 静态未知的类型。”}

当尝试序列化时。

有人知道如何使用 XmlInclude 指向已知的类型提供程序吗?

To start: This is also for REST deserialiaztion, so a custom XmlSerializer is out of the question.

I have a hjierarchy of classes that need to be serializable and deserializable from an "Envelope". It has an arrayelement named "Items" that can contain subclasses of the abstract "Item".

[XmlArray("Items")]
public Item [] Items { get; set; }

Now I need to add XmlArrayItem, but the number is not "fixed". We use so far reflection to find all subclasses with a KnownTypeProvider so it is easy to extend the assembly with new subtypes. I dont really want to hardcode all items here.

The class is defined accordingly:

[XmlRoot]
[KnownType("GetKnownTypes")]
public class Envelope {

but it does not help.

Changing Items to:

[XmlArray("Items")]
[XmlArrayItem(typeof(Item))]
public Item [] Items { get; set; }

results in:

{"The type
xxx.Adjustment
was not expected. Use the XmlInclude
or SoapInclude attribute to specify
types that are not known statically."}

when tyrying to serialize.

Anyone an idea how I can use XmlInclude to point to a known type provider?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

客…行舟 2024-11-06 22:40:25

KnownTypesAttribute 不适用于 XmlSerializer。它仅由 DataContractSerializer 使用。我非常确定您可以在 WCF 中交换序列化器,因为我已经为 DataContractSerializer 做到了这一点。但如果这不是一个选项,您必须自己实现 IXmlSerialized 并在那里处理类型查找。

在取消此解决方案资格之前:您只需为替换 Item[] 的特殊类实现 IXmlSerializing 即可。其他一切都可以由默认序列化器处理。

The KnownTypesAttribute does not work for XmlSerializer. It's only used by a DataContractSerializer. I'm quite sure that you can exchange the serializer in WCF, because I have done that for the DataContractSerializer. But if that's not an option, you have to implement IXmlSerializable yourself and handle type lookup there.

Before disqualifying this solution: You just have to implement IXmlSerializable just for a special class which replaces Item[]. Everything else can be handled by the default serializer.

ヤ经典坏疍 2024-11-06 22:40:25

根据: http:// /social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/83181d16-a048-44e5-b675-a0e8ef82f5b7/

您可以使用不同的 XmlSerializer 构造函数:

new XmlSerializer(typeof(Base), new Type[] { typeof(Derived1), ..});

而不是枚举基本定义中的所有派生类如下所示:

[System.Xml.Serialization.XmlInclude(typeof(Derived1))]
[System.Xml.Serialization.XmlInclude(typeof(Derived2))]
[System.Xml.Serialization.XmlInclude(typeof(DerivedN))]

我认为您应该能够使用 KnownTypeProvider 来填充 XmlSerializer 构造函数中的数组。

According to: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/83181d16-a048-44e5-b675-a0e8ef82f5b7/

you can use different XmlSerializer constructor:

new XmlSerializer(typeof(Base), new Type[] { typeof(Derived1), ..});

Instead of enumerating all derived classes in the base definition like this:

[System.Xml.Serialization.XmlInclude(typeof(Derived1))]
[System.Xml.Serialization.XmlInclude(typeof(Derived2))]
[System.Xml.Serialization.XmlInclude(typeof(DerivedN))]

I think you should be able to use your KnownTypeProvider to fill the array in the XmlSerializer's constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文