473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 11035
I'm not using IPersistable, but I seem to have the same problem.

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

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

Similar topics

2
by: Daniel Faensen | last post by:
As a good OO programmer that I hopefully am I prefer to implement against interfaces rather than classes. This is especially useful when it comes to multiple inheritance which is as you know an...
1
by: Ayende Rahien | last post by:
Two questions: I've a XmlTextWriter that I want to use to build a string in memory. However, when I'm using a StringWriter, the xml comes out at UTF-16, which isn't good for me. Currently, I...
5
by: Hollywood | last post by:
First, is it possible to use XmlArrayItem to declare two data types, DT1 and DT2 where DT2 is a derived class, in an ArrayList as the same ElementName? The following throws a...
1
by: Opa | last post by:
Hi I'm having problem serialization an object instance which contains a public property on the object type My object hierarchy is many levels deep, so for simplicty I created to following which...
2
by: Steven | last post by:
Hello, In my asp.net application, I have the MainForm.aspx.cs (where all the functions are defined) and ClasssificationInfo.cs class. This is the Classification Info class -- using System;...
1
by: Eric Porter | last post by:
Help! Below is some code for a Web Service which has two methods, First() and Second(). However, when the code is run, whichever of the two methods appears second in the source fails with...
3
by: =?Utf-8?B?SWFpbg==?= | last post by:
suppose I have public class A { public List<AContents; } public class B: A { public string nameB;
7
by: =?Utf-8?B?U3dhcHB5?= | last post by:
hi, I m trying to serialize the object of the serializable class DsExportDatabase. But XmlSerializer serializer = new XmlSerializer(typeof(DsExportDatabase)); This line throws me exception...
0
by: jb489 | last post by:
Hi all, Hope I am posting this in the right forum. I seem to be having a problem when using serialization and web services. <b>Scenario:</b> I have built a web service which includes a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.