代码之家  ›  专栏  ›  技术社区  ›  Oliver

对列表进行序列化保留到XML的接口

  •  2
  • Oliver  · 技术社区  · 15 年前

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

    我目前正在处理一个业务对象,它将保存我的所有数据,我们需要将此对象转换为XML和XML。

    我的对象包含一个操作列表(列表…),但有两个操作类型(目前)。 我必须操作类型simpleAction和compositeAction,它们都继承自action,允许它们都保存在操作列表中。

    现在,您可能会看到这个问题,因为接口不能序列化,因为它们不包含任何数据。

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

    一些代码:

      [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
    }
    

    任何帮助都将不胜感激。:)

    3 回复  |  直到 8 年前
        1
  •  4
  •   Mike    15 年前

    您可以使用xmlarayitematribute,正如我们讨论过的那样,创建一个IAction列表并不好,因此创建基类也很好。

    public interface IAction {}
    public abstract class ActionBase : IAction {}
    public class SimpleAction : ActionBase {}
    public class ComplexAction : ActionBase {}
    
    
    [XmlArray("Actions")]
    [XmlArrayItem(typeof(SimpleAction)),XmlArrayItem(typeof(ComplexAction))]
    public List<ActionBase> Actions { get; set; }
    

    实际上,您还可以这样控制XML文件中的元素名:

      [XmlArray("Actions")]
      [XmlArrayItem(typeof(SimpleAction),ElementName = "A")]
      [XmlArrayItem(typeof(ComplexAction),ElementName = "B")]
      public List<ActionBase> Actions { get; set; }
    
        2
  •  2
  •   Michael S. Scherotter    8 年前

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

    我所做的不是抱着

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

    我决定创建一个处理列表的actionsCollection类,但也允许我使用IXMLSerializable重写readXML和writeXML方法,以便处理列表序列化和反序列化的方式。

    [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
    }
    
        3
  •  0
  •   John Saunders Tony    15 年前

    也许如果您从实现接口的公共抽象基类派生两个操作类?

    public interface IAction {}
    public abstract class ActionBase : IAction {}
    public class SimpleAction : ActionBase {}
    public class ComplexAction : ActionBase {}
    

    然后就不用了 List<IAction> 使用 List<ActionBase> .