By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Serializing 'unknown' objects

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.