473,387 Members | 1,504 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,387 software developers and data experts.

C# APP XML Serialization: recognizing type of generic object.

Hi there, I have an issue with serializing C# objects to XML using a generic type using System.Xml.Serialization. Say I have classes defined as below:

Expand|Select|Wrap|Line Numbers
  1. public class ParentClass
  2. {
  3.      private object _arguments;
  4.  
  5.      public object Arguments
  6.      {
  7.           get { return _arguments; }
  8.           set { _arguments = vale; }
  9.      }
  10. }
  11.  
  12. public class SomeArguments
  13. {
  14.      private string _someValue;
  15.  
  16.      public string SomeValue
  17.      {
  18.          get { return _someValue; }
  19.          set { _someValue = value; }
  20.      }
  21. }
Now if I set the Arguments property of ParentClass to an instance of SomeArguments, I need ParentClass to serialize like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<ParentClass>
<Arguments type="SomeArguments">
<SomeValue>blah</SomeValue>
</Arguments>
</ParentClass>

or alternatively:

<?xml version="1.0" encoding="iso-8859-1"?>
<ParentClass>
<Arguments type="SomeArguments">
<SomeArguments>
<SomeValue>blah</SomeValue>
</SomeArguments>
</Arguments>
</ParentClass>

I can sort of get the first functionality by including a [XmlInclude(typeof(SomeArguments))] tag before the definition of the ParentClass class. The problem is, the ParentClass cannot know the type of Arguments. I need the serialization to recognize the type of any class in Arguments. Is this possible? Thanks in advance.
Mar 10 '08 #1
1 1503
nateraaaa
663 Expert 512MB
Try adding

[Serializable] above your class definitions. This will allow your class to be serialized and passed via XML over the web.

Expand|Select|Wrap|Line Numbers
  1.  [Serializable] 
  2. public class Class1
  3. {
  4. }
  5.  
Nathan
Mar 10 '08 #2

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

Similar topics

0
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the...
2
by: Justin Crites | last post by:
I have an object which I want to be serializable. I have marked with with . The object only has a single data member, which is a LinkedList<int>. This linked list is a private member and cannot...
4
by: Joe | last post by:
I would like to call a method of an object before the object is serialized. I currently have a surrogate that I use for deserilization and was thinking I could use one for serialization but that...
5
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
2
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for...
0
by: nobin01 | last post by:
Dear sir; I want ur Help in serialization.I know serialization.I Know binary,soap and xmlserialization also.But i want ur help in following topics.pls help me as soon as possible.I have search in...
6
by: pamela fluente | last post by:
Hi, please find below a very simple code snippet which is giving me the following error: System.Runtime.Serialization.SerializationException was unhandled Message="The constructor to deserialize...
2
by: Peter Duniho | last post by:
I've been learning about mechanisms .NET offers to export data. The initial goal is to see what sorts of ways are available to save an application's state (document, internal database, whatever). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...
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,...

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.