473,725 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best way to serialize unknown data

Hi there,

What is the best way to serialize unknown data?
I have a class that contains a list of parameter objects.
The parameter has a value which can be a simple value of a complex class,
e.g.

class param {
object value;
}

When serializing, I must always have the same name for the value element.

I've looked at 2 approaches:
1) Value is of type object. XmlSerializer MUST know the object's type.
2) Value is of type XmlElement. XmlSerializer doesn't care about content.

Which is better?
1 is easy but has a limitation, 2 is more work.

In addition, the receiver of the xml does not understand type definitions
(it will generate an error) so I cannot have these in my xml.
Does this limit my possibilities to XmlElement IF I want to Deserialize the
same xml?

Any help appreciated,
Michel
Nov 12 '05 #1
5 2655
Michel,

You could inspect the type of the value property and dynamically create an
XmlSerializer instances for every type. You can declare the type and the
element name dynamically using the XmlSerializer constructor that takes an
XmlAttributeOve rrides parameter.

However, constructing an XmlSerializer is a pretty expensive operation, so
this approach carries a lot of overhead if the types are completely random
and don't repeat. I am assuming though, that it's not completely random,
since you're application can only know so many types. Is there a reason you
can't just add A LOT of XmlElement attributes to the class, either at
compile time or using the XmlAttributeOve rrides parameter?

If you're really working with completely arbitratry XML then typing the
property as XmlElement is the best way to go.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader10.wxs .nl...
Hi there,

What is the best way to serialize unknown data?
I have a class that contains a list of parameter objects.
The parameter has a value which can be a simple value of a complex class,
e.g.

class param {
object value;
}

When serializing, I must always have the same name for the value element.

I've looked at 2 approaches:
1) Value is of type object. XmlSerializer MUST know the object's type.
2) Value is of type XmlElement. XmlSerializer doesn't care about content.

Which is better?
1 is easy but has a limitation, 2 is more work.

In addition, the receiver of the xml does not understand type definitions
(it will generate an error) so I cannot have these in my xml.
Does this limit my possibilities to XmlElement IF I want to Deserialize the same xml?

Any help appreciated,
Michel

Nov 12 '05 #2
Hi Christoph,

Thanks for your answer.

As you note, there is a limit to the amount of classes I use, so I can tell
XmlSerializer, either run- or compiletime. That is what I do now (I examine
the classes used before creating the XmlSerializer and register them in
XmlSerializer's constructor). Note I use the "object solution" and serialize
only one object (the one that contains all the others).
I think I'll use your argument that XmlSerializer is expensive and stick to
my current solution.
I haven't looked at XmlAttributeOve rrides but I will.

Thanks again,
Michel

"Christoph Schittko [MVP]" <ch************ ********@austin .rr.com> wrote in
message news:eq******** ******@TK2MSFTN GP11.phx.gbl...
Michel,

You could inspect the type of the value property and dynamically create an
XmlSerializer instances for every type. You can declare the type and the
element name dynamically using the XmlSerializer constructor that takes an
XmlAttributeOve rrides parameter.

However, constructing an XmlSerializer is a pretty expensive operation, so
this approach carries a lot of overhead if the types are completely random
and don't repeat. I am assuming though, that it's not completely random,
since you're application can only know so many types. Is there a reason you can't just add A LOT of XmlElement attributes to the class, either at
compile time or using the XmlAttributeOve rrides parameter?

If you're really working with completely arbitratry XML then typing the
property as XmlElement is the best way to go.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader10.wxs .nl...
Hi there,

What is the best way to serialize unknown data?
I have a class that contains a list of parameter objects.
The parameter has a value which can be a simple value of a complex class, e.g.

class param {
object value;
}

When serializing, I must always have the same name for the value element.
I've looked at 2 approaches:
1) Value is of type object. XmlSerializer MUST know the object's type.
2) Value is of type XmlElement. XmlSerializer doesn't care about content.
Which is better?
1 is easy but has a limitation, 2 is more work.

In addition, the receiver of the xml does not understand type definitions (it will generate an error) so I cannot have these in my xml.
Does this limit my possibilities to XmlElement IF I want to Deserialize

the
same xml?

Any help appreciated,
Michel


Nov 12 '05 #3
Michel,

Note that only the first instantiation of the XmlSerializer is very
expensive. Subsequent ones carry less overhead and you might even be able to
avoid that altogether if you can determine the set of attributes to apply in
the source code or the XmlAttributeOve rrides and keep the XmlSerializer
instance around for the lifetime of your app.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader08.wxs .nl...
Hi Christoph,

Thanks for your answer.

As you note, there is a limit to the amount of classes I use, so I can tell XmlSerializer, either run- or compiletime. That is what I do now (I examine the classes used before creating the XmlSerializer and register them in
XmlSerializer's constructor). Note I use the "object solution" and serialize only one object (the one that contains all the others).
I think I'll use your argument that XmlSerializer is expensive and stick to my current solution.
I haven't looked at XmlAttributeOve rrides but I will.

Thanks again,
Michel

"Christoph Schittko [MVP]" <ch************ ********@austin .rr.com> wrote in
message news:eq******** ******@TK2MSFTN GP11.phx.gbl...
Michel,

You could inspect the type of the value property and dynamically create an
XmlSerializer instances for every type. You can declare the type and the
element name dynamically using the XmlSerializer constructor that takes an XmlAttributeOve rrides parameter.

However, constructing an XmlSerializer is a pretty expensive operation, so this approach carries a lot of overhead if the types are completely random and don't repeat. I am assuming though, that it's not completely random,
since you're application can only know so many types. Is there a reason

you
can't just add A LOT of XmlElement attributes to the class, either at
compile time or using the XmlAttributeOve rrides parameter?

If you're really working with completely arbitratry XML then typing the
property as XmlElement is the best way to go.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader10.wxs .nl...
Hi there,

What is the best way to serialize unknown data?
I have a class that contains a list of parameter objects.
The parameter has a value which can be a simple value of a complex class, e.g.

class param {
object value;
}

When serializing, I must always have the same name for the value element.
I've looked at 2 approaches:
1) Value is of type object. XmlSerializer MUST know the object's type.
2) Value is of type XmlElement. XmlSerializer doesn't care about content.
Which is better?
1 is easy but has a limitation, 2 is more work.

In addition, the receiver of the xml does not understand type definitions (it will generate an error) so I cannot have these in my xml.
Does this limit my possibilities to XmlElement IF I want to

Deserialize the
same xml?

Any help appreciated,
Michel



Nov 12 '05 #4
Hi Christoph,

Thanks for pointing this out.
The only question I have about this is, the client of my COM object is an
asp site.
My asp knowledge is pretty slim, but my guess is different requests live in
different threads.
So, if I have a shared XmlSerializer object, do I need to make the calls to
it thread safe?

Thanks,
Michel

"Christoph Schittko [MVP]" <ch************ ********@austin .rr.com> wrote in
message news:us******** ******@TK2MSFTN GP12.phx.gbl...
Michel,

Note that only the first instantiation of the XmlSerializer is very
expensive. Subsequent ones carry less overhead and you might even be able to avoid that altogether if you can determine the set of attributes to apply in the source code or the XmlAttributeOve rrides and keep the XmlSerializer
instance around for the lifetime of your app.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader08.wxs .nl...
Hi Christoph,

Thanks for your answer.

As you note, there is a limit to the amount of classes I use, so I can tell
XmlSerializer, either run- or compiletime. That is what I do now (I

examine
the classes used before creating the XmlSerializer and register them in
XmlSerializer's constructor). Note I use the "object solution" and

serialize
only one object (the one that contains all the others).
I think I'll use your argument that XmlSerializer is expensive and stick

to
my current solution.
I haven't looked at XmlAttributeOve rrides but I will.

Thanks again,
Michel

"Christoph Schittko [MVP]" <ch************ ********@austin .rr.com> wrote in
message news:eq******** ******@TK2MSFTN GP11.phx.gbl...
Michel,

You could inspect the type of the value property and dynamically create an XmlSerializer instances for every type. You can declare the type and
the element name dynamically using the XmlSerializer constructor that takes
an XmlAttributeOve rrides parameter.

However, constructing an XmlSerializer is a pretty expensive
operation,
so this approach carries a lot of overhead if the types are completely random and don't repeat. I am assuming though, that it's not completely
random, since you're application can only know so many types. Is there a reason
you
can't just add A LOT of XmlElement attributes to the class, either at
compile time or using the XmlAttributeOve rrides parameter?

If you're really working with completely arbitratry XML then typing

the property as XmlElement is the best way to go.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader10.wxs .nl...
> Hi there,
>
> What is the best way to serialize unknown data?
> I have a class that contains a list of parameter objects.
> The parameter has a value which can be a simple value of a complex

class,
> e.g.
>
> class param {
> object value;
> }
>
> When serializing, I must always have the same name for the value

element.
>
> I've looked at 2 approaches:
> 1) Value is of type object. XmlSerializer MUST know the object's type. > 2) Value is of type XmlElement. XmlSerializer doesn't care about

content.
>
> Which is better?
> 1 is easy but has a limitation, 2 is more work.
>
> In addition, the receiver of the xml does not understand type

definitions
> (it will generate an error) so I cannot have these in my xml.
> Does this limit my possibilities to XmlElement IF I want to

Deserialize the
> same xml?
>
> Any help appreciated,
> Michel
>
>



Nov 12 '05 #5
No you don't need do do anything special to make the XmlSerializer thread
safe. It is thread safe as long as you don't register any non-thread-safe
event handlers.
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader08.wxs .nl...
Hi Christoph,

Thanks for pointing this out.
The only question I have about this is, the client of my COM object is an
asp site.
My asp knowledge is pretty slim, but my guess is different requests live in different threads.
So, if I have a shared XmlSerializer object, do I need to make the calls to it thread safe?

Thanks,
Michel

"Christoph Schittko [MVP]" <ch************ ********@austin .rr.com> wrote in
message news:us******** ******@TK2MSFTN GP12.phx.gbl...
Michel,

Note that only the first instantiation of the XmlSerializer is very
expensive. Subsequent ones carry less overhead and you might even be able
to
avoid that altogether if you can determine the set of attributes to apply
in
the source code or the XmlAttributeOve rrides and keep the XmlSerializer
instance around for the lifetime of your app.

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

"Michel" <ms*@virtualsci ences.nl> wrote in message
news:c0******** **@reader08.wxs .nl...
Hi Christoph,

Thanks for your answer.

As you note, there is a limit to the amount of classes I use, so I can tell
XmlSerializer, either run- or compiletime. That is what I do now (I

examine
the classes used before creating the XmlSerializer and register them in XmlSerializer's constructor). Note I use the "object solution" and

serialize
only one object (the one that contains all the others).
I think I'll use your argument that XmlSerializer is expensive and stick to
my current solution.
I haven't looked at XmlAttributeOve rrides but I will.

Thanks again,
Michel

"Christoph Schittko [MVP]" <ch************ ********@austin .rr.com>

wrote in message news:eq******** ******@TK2MSFTN GP11.phx.gbl...
> Michel,
>
> You could inspect the type of the value property and dynamically create
an
> XmlSerializer instances for every type. You can declare the type and

the > element name dynamically using the XmlSerializer constructor that takes
an
> XmlAttributeOve rrides parameter.
>
> However, constructing an XmlSerializer is a pretty expensive

operation,
so
> this approach carries a lot of overhead if the types are completely

random
> and don't repeat. I am assuming though, that it's not completely

random, > since you're application can only know so many types. Is there a reason you
> can't just add A LOT of XmlElement attributes to the class, either
at > compile time or using the XmlAttributeOve rrides parameter?
>
> If you're really working with completely arbitratry XML then typing

the > property as XmlElement is the best way to go.
> --
> HTH
> Christoph Schittko [MVP, XmlInsider]
> Software Architect, .NET Mentor
>
> "Michel" <ms*@virtualsci ences.nl> wrote in message
> news:c0******** **@reader10.wxs .nl...
> > Hi there,
> >
> > What is the best way to serialize unknown data?
> > I have a class that contains a list of parameter objects.
> > The parameter has a value which can be a simple value of a complex
class,
> > e.g.
> >
> > class param {
> > object value;
> > }
> >
> > When serializing, I must always have the same name for the value
element.
> >
> > I've looked at 2 approaches:
> > 1) Value is of type object. XmlSerializer MUST know the object's type. > > 2) Value is of type XmlElement. XmlSerializer doesn't care about
content.
> >
> > Which is better?
> > 1 is easy but has a limitation, 2 is more work.
> >
> > In addition, the receiver of the xml does not understand type
definitions
> > (it will generate an error) so I cannot have these in my xml.
> > Does this limit my possibilities to XmlElement IF I want to

Deserialize
> the
> > same xml?
> >
> > Any help appreciated,
> > Michel
> >
> >
>
>



Nov 12 '05 #6

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

Similar topics

1
2950
by: Michael | last post by:
Hi I anyone have a clue or can solve my problem I would be glad :-) Regards Michael I have a problem with creating an XML-document where the returning data from the webservice, have been serialized. In my program I am calling a webservice which then return the data to the calling program. The program then have to serialize the data and create an XML-document.
14
14306
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
7
6578
by: | last post by:
In the beginning we had Ini files. Later we had registery files. Now have xml files and our read-only myapp.config file. My question now, is what is the best way to store and load user and machine specific settings for a .NET program? And what classes, or code do we have to do this in C#? I don't think that using the myapp.config is the best choice to store since the file might be read-only if the program is started from a CD ROM, or...
7
5987
by: MrNobody | last post by:
I was a Java developer so I'm used to using property files as a means to keep configuration settings for my apps. I'm wondering what options are there with ..NET? Some settings I want to include are like a root directory for a target program on the user's machine my app uses, which they would be prompted to supply at startup. Normally the registry is used for this but I'd rather not use the registry if possible. Isn't there some kind of...
1
1468
by: js | last post by:
Does anybody knows how to solve the problem? I added attribute to the following classes in Microsoft.Practices.EnterpriseLibrary.Data namespace, but I still get the error. Thanks. Database.cs DatabaseFactory.cs DatabaseProviderFactory.cs DBCommandWrapper.cs
2
2542
by: Ariana | last post by:
I have the following situation for an ASP.NET 2.0 web site: - Data is in one XML file - I want to transform the data using an XSLT file before using it - The data is hierarchical - The data will change only occasionally but is quite large and needs to be up to date when it does change - The data will be used to display information in a standard format on every page, but will be filtered on different pages
4
7137
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 Building._Name to Building._BName in case the duplicate name was the issue, but that didn't help. Is there a native way to serialize nested objects, or will I have to write my own? public class LocationCell
2
2699
by: FFMG | last post by:
Hi, I would like to add international support for my site to allow some of my users to translate the site if they really feel the urge to do it. I don't use any CMS, the site was developed, (and refined :)), by me. But I see that various CMSs handle languages very differently. Joomla! for example has a smallish define file that has all the translations. Wordress on the other hand seems to have a function __(...) that is
4
2020
by: Brian | last post by:
HI, I have two sets of data, the largest set of data contains 370 rows... both sets only have two columns. I want to be able to distribute the data with my applaction. The other option, would be to have the data as seprate file(easier if needs updating), but I don't what the user to be able to read the file, just my program should be able to read the file. Any ideas on the best method? thanks, Brian
0
8752
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
9401
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...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8099
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
6702
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
4519
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...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.