locked
XML Serialisation -> "Didn't expect ..." RRS feed

  • Question

  • An exception is thrown when I try to serialize this class: Classdiagram (in reality there are many more derived classes)
    The derived classes are serializable and are only used for initialisation. (e.g. InstallAction ia = new InstallFont("somefont"))

    The exception says something about the serialiser not expecting "InstallFont" (or any other derived class),
    and suggests to use the attribute XmlInclude or SoapInclude.
    But I'm not a big fan of these attributes... Imho the base class shouldn't know about it's derived classes.

    So I tried to avoid the problem by adding a new abstract property (object[] Parameters) which is able to get and set all variables.
    Then I marked all other properties with [XmlIgnore] so they won't be serialized.
    But when I run this code, the same exception is still thrown...

    Is there any workaround for this?
    • Moved by jack 321 Thursday, July 17, 2008 6:04 AM off topic for C# general
    Monday, July 14, 2008 3:54 PM

Answers

All replies

  • And something else:


    I've got a class InstallItem, which is used in InstallItemCollection, which is used in InstallPage.
    When I implement IEnumerable<T> in InstallItemCollection and in InstallPage, I get the following xml output:
    <ArrayOfArrayOfInstallItem><ArrayOfInstallItem>...</ArrayOfArrayOfInstallItem></ArrayOfInstallItem>
    And all other properties aren't serialized.
    (also I have to implement the non generic ICollection for at least one of both classes so the serializer won't whine about "Error cs0136")


    When I delete the IEnumerable interface for InstallItemCollection, I get the following xml output:
    <ArrayOfInstallItemCollection><InstallItemCollection Name="test">...</InstallItemCollection><ArrayOfInstallItemCollection>
    So now it serializes other properties from InstallItemCollection, but still not for InstallPage.
    This is what I expect:
    <InstallPage Name="TestPage"><OtherProperties... /><InstallItemCollection Name="test">...</InstallItemCollection><InstallPage>
    But this would mean I can't use IList<T> or any other interface implementing IEnumerable<T>.

    (I'm getting really really irritated by System.Xml.Serialization...)


    Monday, July 14, 2008 4:58 PM
  • Discussion in Data Platform Development


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, July 17, 2008 6:04 AM
  • This was moved out of the C# forum.

    You can ask this in another forum.

    Or I can move it into a specific MSDN forum if you'd like.

    This is regarding your example:

     InstallAction ia = new InstallFont("somefont")
    

     

    Thanks!
    Ed Price a.k.a User Ed, Microsoft Experience Program Manager (Blog, Twitter, Wiki)
    Friday, December 30, 2011 5:59 AM