469,964 Members | 1,474 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

C# XML and binary Serialization - XmlInclude being ignored ?

I am developing a windows service which utilizes the following classes:
Expand|Select|Wrap|Line Numbers
  1. interface IPersistable;
  2.  
  3. [Serializable()]
  4. [XmlInclude(typeof(Task))]
  5. abstract class PersistableObject : IPersistable;
  6.  
  7. [Serializable()]
  8. [XmlInclude(typeof(AccountExpirationWarningTask))]
  9. [XmlInclude(typeof(EnableAccountTask))]
  10. abstract class Task : PersistableObject;
  11.  
  12. [Serializable()]
  13. class EnableAccountTask : Task;
  14.  
  15. [Serializable()]
  16. class AccountExpirationWarningTask : Task;
  17.  
The service uses binary serialization to send instances of EnableAccountTask and AccountExpirationWarningTask to a remote object cache and XML serialization to commit instances of classes to a remote SQL 2005 database.

I have been using XML serialization with these classes for some time without any problems and have only just implemented binary serialization. The binary serialization implementation required the addition of the IPersistable interface (which did not exist previously) and the Serialization() attribute on each class. XML serialization worked perfectly prior to these additions.

After making these modifications, binary serialization works perfectly but XML serialization appears to be broken. When attempting to XML serialize an array of PersistableObject[] containing instances of AccountExpirationWarningTask and/or EnableAccountTask, the following exception is thrown:
Expand|Select|Wrap|Line Numbers
  1. System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type Task was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
  2.  
As you can see from my code above, I am using XmlInclude. While debugging, I was able to verify (using array[0].GetType().BaseType.GetCustomAttributes()) that XmlInclude is an attribute of the Task class and the PersistableObject classes, and that the correct types are being included.

My XML serialization code is as follows:
Expand|Select|Wrap|Line Numbers
  1. private bool Commit(PersistableObject[] objects)
  2. {
  3.     System.IO.MemoryStream stream =
  4.         new System.IO.MemoryStream();
  5.     System.Xml.Serialization.XmlRootAttribute xmlRoot =
  6.         new System.Xml.Serialization.XmlRootAttribute();
  7.     xmlRoot.ElementName = "ObjectList";
  8.     System.Xml.Serialization.XmlSerializer serializer =
  9.         new System.Xml.Serialization.XmlSerializer(typeof(PersistableObject[]), xmlRoot);
  10.     serializer.Serialize(stream, objects);
  11.     ...
  12. }
  13.  
The only possible cause I can think of is that the addition of IPersistable is causing problems, but it is a necessity as my caching components are in another project and I cannot create a circular dependency. I have not seen anything to indicate that Serializable() and XmlInclude() are mutually exclusive, so I don't see why that would be an issue.

The application performs binary serialization prior to XML serialization, however XML serialization still fails if I attempt to perform XML serialization first.

Does anyone have any ideas what might cause this problem? I'm quite stuck.
Oct 1 '07 #1
1 10635
I'm not using IPersistable, but I seem to have the same problem.

I guess it is something else.
Oct 7 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Daniel Faensen | last post: by
1 post views Thread by Ayende Rahien | last post: by
5 posts views Thread by Hollywood | last post: by
2 posts views Thread by Steven | last post: by
3 posts views Thread by =?Utf-8?B?SWFpbg==?= | last post: by
7 posts views Thread by =?Utf-8?B?U3dhcHB5?= | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.