473,769 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# XML and binary Serialization - XmlInclude being ignored ?

1 New Member
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 EnableAccountTa sk and AccountExpirati onWarningTask 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 PersistableObje ct[] containing instances of AccountExpirati onWarningTask and/or EnableAccountTa sk, 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().Base Type.GetCustomA ttributes()) that XmlInclude is an attribute of the Task class and the PersistableObje ct 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 11100
bojanskr
1 New Member
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
10773
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 issue and not supported in languages as Java or C# - for good reasons. I have an object graph I want to persist using the XML serialization features of the .NET framework. The interface/class hierarchy of my business model resembles the following...
1
3174
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 solved the problem by using XmlDocument, but it seems a waste to use that if I can use XmlWriter. I tried to spesify the encoding, and then it complained about needing a Stream and not a TextWriter. I *must* have this in memory, writing to file is...
5
5809
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 "System.InvalidOperationException" exception when creating the serializer. public virtual ArrayList Test { get { return m_alTest; } set { m_alTest = value; }
1
2640
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 produces the same error Let's say there is a class called ParkingSpot with a public member Vehicle having an object type of object For simplicity, let's say that Vehicle could be anything, hence I create a new object of Car typ and assign it to...
2
10054
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; using System.Xml.Serialization ; namespace MyAppl
1
4391
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 "System.InvalidCastException: Specified cast is not valid" in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5 _ArrayOfAnyType(Object o) of the temporary C# DLL that Web Service processing creates when serializing data.
3
4925
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
263
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 that "There was an error reflecting type " wherein i have made to the class DsExportDatabase.
0
2622
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 multitude of web methods. All the web methods have various parameters, but one parameter which they all have in common is an 'object' parameter. This is due to the fact that the web service works differently according to the type of object being...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9999
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.