XmlSerializer and Persistance

T

Trecius

Hello, Newsgroupians:

I am trying to serialize an instantiated object using XmlSerializer. I
would like to serialize the object to an XML file; however, I'd like it to be
in a subcategory in the XML file, for I'd like to save other settings in the
XML file. Is it possible to do this using XmlSerializer?

The class, or type, of my object I'd like to serialize is "Calculator."

Here's an example of my XML file.

<?xml version="1.0"?>
<CalcLibrary>
<Calculator xmlns:xsd="GAppCalculator">
<Name>GEXI</Name>
<Buttons>42</Buttons>
...
</Calculator>
<Calculator xmlns:xsd="GAppCalculator">
<Name>KEK</Name>
<Buttons>50</Buttons>
...
</Calculator>
</CalcLibrary>
<SomeOtherSettingsIWantToSave>
...
</SomeOtherSettingsIWantToSave>

Here's my second question really quickly. Suppose I have many calculators
to store. How can I deserialize a specific calculator? I've seen
documentation that deserializes an object, but the examples I've seen they
are only serializing ONE object and then deserializing that same object. How
can I specify that I would like to deserialize the calculator with the name
"KEK" and not deserialize all the other calculators in the XML file?

Thank you, all, for your help.


Trecius
 
M

Marc Gravell

I'd like it to be in a subcategory in the XML file
...
How
can I specify that I would like to deserialize the calculator with the name
"KEK" and not deserialize all the other calculators in the XML file?

Basically, you'd need to do some work with XmlWriter to write multiple
items to the stream. I seem to recall that XmlWriter is very
possessive about streams and tends to Close() them, but you can get
around this with a non-closing stream (Jon Skeet has one in his
MiscUtil library [google]).

Ditto reading (with XmlReader). If the data volume is moderate, you
could load into a XmlDocument and use a node-reader, but this won't
work for large (many-meg) docs. For the worst-case (XmlReader
directly), unfortunately your requirement forces you to peek *inside*
the object you want to deserialize, which is a pain - if the name was
duplicated outside it would simplify things somewhat:
<Calc Name="KEK">
<Calculator xmlns:xsd="GAppCalculator">
<Name>KEK</Name>
<Buttons>50</Buttons>
...
</Calculator>
</Calc>

I can come up with some more complete XmlReader/XmlWriter examples if
you need?

Marc
 

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