Serialization of objects

F

Flack

Hey guys,
Lets say I have an object, MyObject, that has a couple of public properties.
I want to serialize this object and include it in a larger xml message. So,
I have some elements in the xml message that have nothing to do with the
object, whose values are retrieved elsewhere, and some values which are
gotten directly from MyObject.

All of the serialization examples I have seen deal with simply serializing
the entire object to its own xml format. Any suggestions on how to achieve
what I described?

Here is an example of what I am trying to achieve:
- MyObject has properties Name and Count.
- SessionID is an ID which is not stored in the object.
The xml I am looking to create is:
<Request>
<SessionID>12345</SessionID>
<ObjectValues>
<Name>Test</Name>
<Count>2</Count>
</ObjectValues>
</Request>

Notice how the object type, MyObeject, is not used in the xml and that the
root name is also not related to the object. Basically, the info in the
object just needs to be used. Do I have to create such xml manually, using
an XmlWriter? I was hoping there was some way I can define what info from
what source should be used when creating the xml.

Thanks.
 
B

Bob Powell [MVP]

In serialization, objects are responsible for their own encoding and
decoding. The serialization system in .NET is designed to enable an object
to persist itself fully.

If your properties require serialization they must be able to manage their
own encoding and decoding which may mean creating custom XML serialization
to satisfy your requirements.

Alternatively, you can use a Memento pattern implementation that provides an
object which creates a serialization stream on behalf of the object being
encoded.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top