1

I've got two (different) C# systems which communicate by XML. We have agreed on an XML format.

The next thing I want to do is serialize a class:

  public class Parent
  {
    public IChild Child { get; set; }
    public string Name { get; set; }
  }
  public interface IChild
  {
    string Name { get; set; }
  }
  public class Girl:IChild
  {
    public string Name { get; set; }
    public string FavDoll { get; set; }
  }
  public class Boy : IChild
  {
    public string Name { get; set; }
    public string FavCar { get; set; }
  }

Result when using IXmlSerializer:

When using IXmlSerializable I get a exception there is no constructor on the Interface IChild (wellwaddayaknow).

Ok, fixed that (thanks @Giedrius)

And less ns thanks to @RobertH

ref

<Parent>
  <Child>
    <Boy>
      <Name>Bill</Name>
      <FavCar>Chevvy</FavCar>
    </Boy>
  </Child>
</Parent>

When using DataContract I get a xmlns has a reference to the namespace. Which isn't present on the other system. So when deserializing I get a exception the reference isn't there.

<Parent xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Child xmlns:d2p1="TestConsole, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" i:type="d2p1:TestConsole.Boy">
    <FavCar xmlns="http://schemas.datacontract.org/2004/07/TestConsole">Chevvy</FavCar>
    <Name xmlns="http://schemas.datacontract.org/2004/07/TestConsole">Bill</Name>
  </Child>
</Parent>

Is my only option to use XDocument and generate a lot of loc's?

Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
  • 2
    You can instruct the serializer to generate the XML without the namespace, if that helps... – Icarus Aug 28 '12 at 13:31
  • Not sure, can I instruct the deserializer to understand that xml and turn it into poco's? – Ralf de Kleine Aug 28 '12 at 13:34
  • 1
    similar question: http://stackoverflow.com/questions/1333864/xml-serialization-of-interface-property – Giedrius Aug 28 '12 at 13:35
  • @rdkleine I don't know. You could try and let us/me know. – Icarus Aug 28 '12 at 13:37
  • Will do. Can you give a hint on how to 'instruct the serializer to generate the XML w/o the namespace'? – Ralf de Kleine Aug 28 '12 at 13:40
  • @Giedrius yes, have seen some posts on 'hiding' the properties for the serializer. It seems like a solution but it would seriously mutilate my code and would rather look for other option. – Ralf de Kleine Aug 28 '12 at 13:43
  • @rdkleine - there offered other methods too, not only hiding, like injecting object instances during datacontract serialization. – Giedrius Aug 28 '12 at 13:46
  • btw, have you thought to use json instead of xml? it is more compact and it may have baked in solution for this. – Giedrius Aug 28 '12 at 13:50
  • 1
    @rdkleine I use the following to omit namespaces: `var ns = new XmlSerializerNamespaces(); ns.Add("", "");` – Robert H Aug 28 '12 at 13:55
  • @Giedrius You are right, didn't pay good enough attention. Fixed the interface issue. Now trying to get the result through deserializing. – Ralf de Kleine Aug 28 '12 at 13:56
  • @rdkleine to supress NS, all you need to do is: `[DataContract(Namespace="")]` if using the `DataContracSerializer` – Icarus Aug 28 '12 at 14:00
  • @Giedrius your comment (similar question) worked for me, want the credits add an answer. – Ralf de Kleine Aug 30 '12 at 08:54

2 Answers2

1

According to this question there are several ways to solve interface properties issue:

  • Hide interface property and deal with it in another property
  • Implement IXmlSerializable
  • Modify your property to use a wrapping type
  • Using reflection with the DataContractSerializer to add interface property current value type to known types
Community
  • 1
  • 1
Giedrius
  • 8,430
  • 6
  • 50
  • 91
0
public class Parent
{
    [XmlElement(Type = typeof(Girl))]
    [XmlElement(Type = typeof(Boy))]
    public IChild Child { get; set; }
}
Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35
  • Tried your answer but getting "*Cannot serialize member TestConsole.Parent.Child of type TestConsole.IChild because it is an interface.*" – Ralf de Kleine Aug 28 '12 at 14:19