<p>I have a simple type that explicitly implemets an Interface.</p>

<pre><code>public interface IMessageHeader
{
    string FromAddress { get; set; }
    string ToAddress   { get; set; }
}

[Serializable]
public class MessageHeader:IMessageHeader
{
  private string from;
  private string to;

  [XmlAttribute("From")]
  string IMessageHeade.FromAddress
  {
    get { return this.from;}
    set { this.from = value;}
  }

 [XmlAttribute("To")]
 string IMessageHeade.ToAddress
 {
    get { return this.to;}
    set { this.to = value;}
 }
}
</code></pre>

<p>Is there a way to Serialize and Deserialize objects of type IMessageHeader??</p>

<p>I got the following error when tried</p>

<p>"Cannot serialize interface IMessageHeader"</p>

[{'answer_id': 59992, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': -1, 'selected': False, 'text': '<p>You can create an abstract base class the implements IMessageHeader and also inherits MarshalByRefObject</p>\n'}, {'answer_id': 59993, 'author': 'Brannon', 'author_id': 5745, 'author_profile': 'https://Stackoverflow.com/users/5745', 'pm_score': 0, 'selected': False, 'text': "<p>No, because the serializer needs a concrete class that it can instantiate.</p>\n\n<p>Given the following code:</p>\n\n<pre><code>XmlSerializer ser = new XmlSerializer(typeof(IMessageHeader));\n\nIMessageHeader header = (IMessageHeader)ser.Deserialize(data);\n</code></pre>\n\n<p>What class does the serializer create to return from Deserialize()?</p>\n\n<p>In theory it's possible to serialize/deserialize an interface, just not with XmlSerializer.</p>\n"}, {'answer_id': 59994, 'author': 'Jon', 'author_id': 4764, 'author_profile': 'https://Stackoverflow.com/users/4764', 'pm_score': 0, 'selected': False, 'text': "<p>Try adding IXmlSerializable to your IMessageHeader declaration, although I don't think that will work.</p>\n\n<p>From what I recall, the .net xml serializer only works for concrete classes that have a default constructor.</p>\n"}, {'answer_id': 60005, 'author': 'Steve Morgan', 'author_id': 5806, 'author_profile': 'https://Stackoverflow.com/users/5806', 'pm_score': 0, 'selected': False, 'text': "<p>The issue stems from the fact that you can't deserialize an interface but need to instantiate a concrete class.</p>\n\n<p>The XmlInclude attribute can be used to tell the serializer what concrete classes implement the interface.</p>\n"}, {'answer_id': 60043, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 3, 'selected': True, 'text': "<p>You cannot serialize IMessageHeader because you can't do Activator.CreateInstance(typeof(IMessageHeader)) which is what serialization is going to do under the covers. You need a concrete type.</p>\n\n<p>You can do typeof(MessageHeader) or you could say, have an instance of MessageHeader and do </p>\n\n<pre><code>XmlSerializer serializer = new XmlSerializer(instance.GetType())\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/59986', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1647/']