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

Serializing 'unknown' objects

All,

I have quite a challenging task ahead of me. I need to write an object model
(for code access) based on a schema, which cannot be made into a dataset
because of it's complexity.
So I created a couple of objects and serializing it into XML based upon the
schema works perfectly.

The XML / Schema looks something like this:

<? xml ?>
<Root>
<KnownElement1></KnownElement1>
<KnownElement2></KnownElement2>
...
<WeirdElementContainer>
<WeirdElement>
</WeirdElement>
</WeirdElementContainer>
</Root>

The only problem lies with the 'WeirdElement'. This tag contains different
information per schema. Since I am a very lazy programmer I just want to
write the object model once, and not per schema.

I thought of a solution to my problem. First I could create a base
'WeirdElement' class and inherit my other objects. So I would get something
like this:

[System.Xml.Serialization.XmlInclude(typeof(NotSoWe irdElement))]
public class WeirdElement
{
}

But, when serializing this I would get a "xsi:type=" attribute in the
WeirdElement XML tag and that is not desirable because of the schema.

What I would really like to do is create a custom serializer per
WeirdElement class. In pseudo code it goes something like this:

XmlSerializer xs = new XmlSerializer(typeof(Root));

[System.Xml.Serialization.UseXmlSerializer(typeof(M yWeirdElementSerializer))]
public class WeirdElement
{
}

xs.Serialize(myRootObject);

I would basically like to run my own custom serializer whenever the 'normal'
serializer encounters my WeirdElement property. That way I could create or
parse the XML according to the rules set by the current schema.

If anyone knows how to do this or how to solve this problem any other way,
please let me know...
Thanks in advance,
Ivo Bronsveld
Lead Developer e-Clubs
www.e-clubs.nl
<< When replying directly to me, please remove the nospam from my e-mail>>
Nov 12 '05 #1
1 2050
All,

Just a follow-up on how I did manage to solve this problem, for all you out
there who need to solve a similar problem.

I did the following:

I created a number of classes:
- A Generic Parser class which handles the serialization.
- A custom attribute which contains a mapping to Schema name and version.
- A base 'WeirdElementClass' which can be inherited from

In the Generic Parser class I handle the UnknownElement event from the
serializer, since the container class does contain the base
'WeirdElementClass' but it is marked with the XmlIgnore attribute. In this
event I check for the correct element, just to be sure. Then I use
reflection to find the specific schema / verion attribute which I attached
to the correct 'WeirdElementClass'. When found I can deserialize this class
and add its instance to the property to the container class.

Serializing is pretty straight forward, after serializing the Root object to
an XmlDocument I can manually serialize the 'WeirdElementClass' and add it
to the xml at the correct location.

If anyone wants to know more, please let me know.

Ivo Bronsveld

"Ivo Bronsveld" <no**************@bts.net> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...
All,

I have quite a challenging task ahead of me. I need to write an object
model (for code access) based on a schema, which cannot be made into a
dataset because of it's complexity.
So I created a couple of objects and serializing it into XML based upon
the schema works perfectly.

The XML / Schema looks something like this:

<? xml ?>
<Root>
<KnownElement1></KnownElement1>
<KnownElement2></KnownElement2>
...
<WeirdElementContainer>
<WeirdElement>
</WeirdElement>
</WeirdElementContainer>
</Root>

The only problem lies with the 'WeirdElement'. This tag contains different
information per schema. Since I am a very lazy programmer I just want to
write the object model once, and not per schema.

I thought of a solution to my problem. First I could create a base
'WeirdElement' class and inherit my other objects. So I would get
something like this:

[System.Xml.Serialization.XmlInclude(typeof(NotSoWe irdElement))]
public class WeirdElement
{
}

But, when serializing this I would get a "xsi:type=" attribute in the
WeirdElement XML tag and that is not desirable because of the schema.

What I would really like to do is create a custom serializer per
WeirdElement class. In pseudo code it goes something like this:

XmlSerializer xs = new XmlSerializer(typeof(Root));

[System.Xml.Serialization.UseXmlSerializer(typeof(M yWeirdElementSerializer))]
public class WeirdElement
{
}

xs.Serialize(myRootObject);

I would basically like to run my own custom serializer whenever the
'normal' serializer encounters my WeirdElement property. That way I could
create or parse the XML according to the rules set by the current schema.

If anyone knows how to do this or how to solve this problem any other way,
please let me know...
Thanks in advance,
Ivo Bronsveld
Lead Developer e-Clubs
www.e-clubs.nl
<< When replying directly to me, please remove the nospam from my e-mail>>

Nov 12 '05 #2

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

Similar topics

0
by: MicFra | last post by:
hi, i got a little problem with pdf-serializing after updating from cocoon 2.1.2 to 2.1.4. following scenario: - using serializer org.apache.cocoon.serialization.FOPSerializer, with name...
1
by: Christopher Pragash | last post by:
Hello all, I'm trying to persist an object of type Arraylist into XML using the following the XMLSerializer and it returns an "Unknown" Error. I'm able to serialize and deserialize the arraylist...
2
by: Chris Smith | last post by:
I bet this question has been asked quite a bit, but if someone can point me in the right direction, I would be very grateful. I would like to write a assembly (dll) that one of the functions will...
4
by: John Vottero | last post by:
When a class contains a TimeSpan property, XmlSerializer doesn't work. A TimeSpan property is serialized like: <MySpan /> I've read a number of posts that talk about why this happens and how...
2
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: +...
4
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm...
0
by: rein.petersen | last post by:
Hi All, I've been working on a means to create dynamic asemblies (modules actually) from parsing XSD schemas which creates a perfect .net representation of the schema at hand (dynamically). ...
47
by: Max | last post by:
Due to the behaviour of a particular COM object, I need to ensure that a request for a particular ASP page is finalized before another request for the page is processed. Does IIS have a way to...
3
by: Jeremy | last post by:
I've created a serializable class and put attributes around all the properties that should be serialized. I return the class from a web service, but my problem is that the wsdl for the web service...
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...
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...
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...

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.