序列化一个 List 包含一个到 XML 的接口 [英] Serializing a List hold an interface to XML

查看:45
本文介绍了序列化一个 List 包含一个到 XML 的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读,但我没有找到解决我问题的方法

I have been reading around but I have not come across a solution to my problem

我目前正在使用一个业务对象来保存我的所有数据,我们需要将此对象与 XML 相互转换.

I am currently working with a Business Object that will hold all my data and we need to convert this object to and from XML.

我的对象包含一个动作列表(列表...),但有 2 种动作类型(目前).我必须使用操作类型 SimpleAction 和 CompositeAction,它们都继承自 IAction,允许它们都保存在 Actions 列表中.

My object holds a list of Actions (List...), but there are 2 action types (for now). I have to action types SimpleAction and CompositeAction, and they both inherit from IAction allowing them to both be held in the Actions list.

现在您可能会看到问题,因为接口无法序列化,因为它们没有数据.

Now you can probably see the problem as Interfaces cannot be serialized as they hold no data.

如何使用一些示例代码编写一个获取该对象类型并执行然后使用正确类型序列化对象的类或序列化程序?

How, with maybe some sample code, do I write a Class or Serializer that gets that object type and performs then serializes object with the correct type?

一些代码:

  [XmlArray("Actions")]
  public List<IAction> Actions { get; set; }

  public interface IAction
   {
     int ID { get; set; }

     ParameterCollection Parameters { get; set; }

     List<SectionEntity> Validation { get; set; }

     TestResultEntity Result { get; set; }

     string Exception { get; set; }
   }

[XmlType("A")]
public class SimpleActionEntity : IAction
{
    #region IAction Members

    [XmlAttribute("ID")]
    public int ID { get; set; }

    [XmlIgnore]
    public ParameterCollection Parameters { get; set; }

    [XmlIgnore]
    public List<SectionEntity> Validation { get; set; }

    [XmlIgnore]
    public TestResultEntity Result { get; set; }

    [XmlElement("Exception")]
    public string Exception { get; set; }

    #endregion
}

任何帮助将不胜感激.:)

Any help would be greatly appreciated. :)

推荐答案

好的,我已经创建了一个我觉得可以很好地满足我想要的解决方案.

Ok I have created a solution that I feels does what I want pretty well.

我所做的不是持有

 [XmlArray("Actions")]
 public List<IAction> Actions { get; set; }

我决定创建一个 ActionsCollection 类来处理列表,但也允许我使用 IXMLSerializable 来覆盖 ReadXml 和 WriteXML 方法,以便我可以处理列表的序列化和反序列化方式.

I decided to create an ActionsCollection class that handled the List BUT also allowed me to use IXMLSerializable to override the ReadXml and WriteXML methods so that I could handle the way the list is Serialized and Deserialized.

[XmlElement("Actions")]
public ActionCollection Actions { get; set; }

public class ActionCollection: CollectionBase, IXmlSerializable
{
    #region IList Members
      ...
    #endregion

    #region ICollection Members
     ...
    #endregion

    #region IEnumerable Members
    ...
    #endregion

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
       //TODO
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        foreach (IAction oAction in List)
        {       
                XmlSerializer s = new XmlSerializer(oAction.GetType());
                s.Serialize(writer, oAction);
        }
    }

    #endregion
}

这篇关于序列化一个 List 包含一个到 XML 的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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