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

序列化列表包含XML接口

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

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

    现在,您可能可以看到问题,因为接口无法序列化,因为它们不包含数据。

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

    一些代码:

      [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 回复  |  直到 17 年前
        1
  •  4
  •   Mike    17 年前

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

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

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

    我决定创建一个ActionsCollection类来处理List,但也允许我使用IXMLSerialize来覆盖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    17 年前

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

    List<IAction> List<ActionBase> .