如何反序列化的具体实现从XML抽象类 [英] How to deserialize concrete implementation of abstract class from XML

查看:219
本文介绍了如何反序列化的具体实现从XML抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类,一对夫妇的具体实现。这需要序列化到XML以发送到另一个系统 - 这是工作的罚款。不过,我也需要能够反序列化相同的XML结构回来。不管我怎么努力,我似乎​​没有能够做到这一点。我的阶级结构如下:

I have an abstract class with a couple of concrete implementations. This needs serializing to XML in order to send to another system - this is working fine. However, I also need to be able to deserialize the same XML structure back. No matter what I try, I don't seem to be able to do this. My class structure is as below:

抽象类:

[XmlIncludeAttribute(typeof(ConcreteFooOne))]
[XmlIncludeAttribute(typeof(ConcreteFooTwo))]
[XmlIncludeAttribute(typeof(ConcreteFooThree))]
[XmlRoot(ElementName = "FooData", Namespace="http://foo.bar")]
public abstract partial class AbstractFoo
{
    // Some abstract props etc.
}

具体类实例:

public partial class ConcreteFooOne : AbstractFoo
{
    // Some properties, constructor etc.
}

XML根举例:

<FooData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ConcreteFooOne" RequestResponse="Request" xmlns="http://foo.bar">

仅包含XML根为例,因为这似乎是那里的问题。现在我可以序列化很好,但在反序列化,如果我反序列化传递的抽象类型,当然,我得到一个异常,说明输入AbstractFoo是抽象的。所以我只是改变了逻辑,这样反而具体类型(ConcreteFooOne在这种情况下)被传递到串行器。现在,我得到一个http://foo.bar'>是没有预料到。我是presuming,这是因为序列化不知道该怎么根节点应该是什么?

Only included XML root as example as this appears to be where the issue is. Now I can serialize fine, but on the deserialization, if I deserialize by passing in the abstract type, I of course get an exception stating that type "AbstractFoo" is abstract. So I simply changed the logic so that instead the concrete type (ConcreteFooOne in this case) is passed to the serializer. Now I get a "http://foo.bar'> was not expected". I am presuming that this is because the serializer doesn't know what to root node should be?

我对抽象类定义,因为这将是相同的所有的具体实现根节点。具体类型由requestResponse则属性来定义(或xsi:type属性可以工作了,如果它是present因为这给了我们实际的类型名称)。有没有一种方法,使串行拿起什么是需要切断抽象类还是我彻底对这个错误的方式?

I have the root node defined on the abstract class as this will be the same for all concrete implementations. The concrete type is defined by the "RequestResponse" attribute (or the xsi:type attribute can work too if it is present as that gives us the actual type name). Is there a way to make the serializer pick up what is required off the abstract class or am I going completely the wrong way about this?

  • 请注意,我不能改变的阶级结构太多,因为它是基于非常密切的关由第三方提供的一些XML模式。

在此先感谢任何人的帮助,这,这将是更AP preciated。

Thanks in advance for anyone's help with this, it would be much appreciated.

推荐答案

添加[XmlRoot(的ElementName =FooData,命名空间= HTTP ://foo.bar 的)]的子类

Add [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")] to the sub-classes

下面是一个例子,我做:

here is an example I made:

  [XmlIncludeAttribute(typeof(ConcreteFooOne))]
  [XmlIncludeAttribute(typeof(ConcreteFooTwo))]
  [XmlIncludeAttribute(typeof(ConcreteFooThree))]
  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]
  public abstract partial class AbstractFoo
  {
    // Some abstract props etc.
  }

  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]
  public class ConcreteFooOne : AbstractFoo
  {
    public int MyProp { get; set; }
  }
  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]
  public class ConcreteFooTwo : AbstractFoo
  {

  }
  [XmlRoot(ElementName = "FooData", Namespace = "http://foo.bar")]
  public class ConcreteFooThree : AbstractFoo
  {

  }

  class Program
  {
    static void Main(string[] args)
    {
      var serializer = new System.Xml.Serialization.XmlSerializer(typeof(AbstractFoo));
      using (var stream = new FileStream("test.txt", FileMode.OpenOrCreate))
      {
        serializer.Serialize(stream, new ConcreteFooOne() { MyProp = 10 });
        stream.Flush();
      }


      using (var stream = new FileStream("test.txt", FileMode.OpenOrCreate))
      {
        var c = serializer.Deserialize(stream);
      }
    }
  }

这篇关于如何反序列化的具体实现从XML抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆