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

how to serialize a generic field object

Hi everybody.
I have to serialize to XML a class like this:

public class MyClass{
string myString;
object myObject;
}

myObject can be a number(int, float, double), a date or a string. Any
suggestion?

May 22 '07 #1
5 2074
Hi,

where is the problem?

XmlSerializer will serialize your object property as <MyObject
xsi:type="xsd:int">12</MyObjector
<MyObject xsi:type="xsd:string">teststring</MyObject>

You should provide public properties and a default constructor

May 22 '07 #2
On 22 Mag, 12:34, Roman Wagner <roman.wag...@gmail.comwrote:
Hi,

where is the problem?

XmlSerializer will serialize your object property as <MyObject
xsi:type="xsd:int">12</MyObjector
<MyObject xsi:type="xsd:string">teststring</MyObject>

You should provide public properties and a default constructor
I tried but I receive an "Error reflecting type MyClass" exception.
This is what I wrote:

public class MyClass
{
[XmlAttribute("Description")]
public string description;
[XmlAttribute("Value")]
public object Value;
}

If I write

public class MyClass
{
[XmlAttribute("Description")]
public string description;
[XmlAttribute("Value")]
public int Value;
}
It works.


May 22 '07 #3
[Serializable()]
public class MyClass
{
private string _Description;
private object _Value;

public MyClass() {}

public string Description
{
get { return _Description; }
set { _Description = value; }
}

public string Value
{
get { return _Value; }
set { _Value = value; }
}
}

May 22 '07 #4
On 22 Mag, 13:27, Roman Wagner <roman.wag...@gmail.comwrote:
[Serializable()]
public class MyClass
{
private string _Description;
private object _Value;

public MyClass() {}

public string Description
{
get { return _Description; }
set { _Description = value; }
}

public string Value
{
get { return _Value; }
set { _Value = value; }
}


I don't think the problem can be solved using properties. As i said if
i specify the type of every fields it works. Of course I used the
attribute [Serializable], I 've just forgotten to write it in the
example.
[Serilizable]
public class MyClass
{
[XmlAttribute("Description")]
public string description;
[XmlAttribute("Value")]
public object Value;
}

May 22 '07 #5
Hmm ok,

i see. The problem is the XmlAttribute which can only be applied to
simple types not to complex types like object.
Use XmlElement.

May 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
0
by: Tim_Mac | last post by:
hi, i am able to serialize a normal generic List<> no problem. But when i try to serialize a Dictionary<string,string>, i get an exception as shown below: "The type...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
3
by: fstorck | last post by:
Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: <code> class MyBase :...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
1
by: Pascal | last post by:
Hi everybody. I have to serialize to XML a class like this: public class MyClass{ string myString; object myObject; } myObject can be a number(int, float, double), a date or a string. Any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: 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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.