473,372 Members | 1,293 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,372 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 2048
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...
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: 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
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?
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...

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.