Dumping Object Data to File

  • Thread starter Thread starter Flynn Arrowstarr
  • Start date Start date
F

Flynn Arrowstarr

I am using a class in C# to store application settings,
but I'd like to be able to write those settings to a
file. According to a book I have for the desktop
Framework, I need to Serialize the class, then use the
GetObjectData method of the ISerializable interface to
define the properties I want to be serialized.

Only problem is, System.Runtime.Serialization doesn't
seem to be supported by the Compact Framework. Any ideas
how I can write the Object properties I want to a
text/XML/binary file? Thanks =)

Flynn
 
well I implemented it myself ;-)
I think you've no choice.
at last the XmlReader & XmlWriter do exist on PPC !
 
It depends on how complex the class is, but you need to come up with a way
to turn the class into a byte array that you then persist to a file.

-Chris
 
Hi, Chris =)

Thanks for the reply. I'm just getting my feet wet in
the .NET CF, so bear with me. I'm really starting to get
the hang of things I think.

I referenced the post you mentioned. My class is fairly
simple, so I can get the byte values for most things.
However, there are two strings in the list of properties
(the rest are mostly int). The two strings hold the path
to the executable, and the path + filename of the
database. So, to find the number of bytes, that would be
string.Length * 2, correct?

The first time the application runs, it checks to see if
the .ini file exists. If not, it generates a class
(AppSettings in my program) and populates it with some
default values. Those defaults are then written to
the .ini file. Using class Foo below, how would that code
look?

|public class Foo
|{
| private byte[] m_bytes = new byte[8];
|
| // ctors
| public Foo(){}
| public Foo(byte[] bytes) { Buffer.BlockCopy(m_bytes,
| bytes, 0); }
|
| // properties
| public uint X
| {
| get{ return BitConverter.ToUInt32(m_bytes[0]); }
| set{ Buffer.BlockCopy(m_bytes, BitConverter.GetBits
| (value), 0); }
| }
| public uint Y
| {
| get{ return BitConverter.ToUInt32(m_bytes[4]); }
| set{ Buffer.BlockCopy(m_bytes, BitConverter.GetBits
| (value), 4); }
| }
|
| // operators
| public static implicit operator byte[](Foo f)
| {
| return f.m_bytes;
| }
| public static implicit operator Foo(byte[] bytes)
| {
| return new Foo(bytes);
| }
|}

Thanks again for all your help, Chris =)

Flynn
 
Hi, Lloyd

I understand that DataSet in the CF supports outputting
to an XML file (through XmlReader and XmlWriter, as you
mentioned). However, how do I get the class properties
into the DataSet? From there it looks like I can auto-
generate the XmlSchema, so I'm not too worried about
that. Thanks =)

Flynn
 
I need to do a similar storage, but thinking how to find the right
place for the a large xml data file so that it's not hanging around in
the root directory. What is the right pace for application data files?
How do you determine that directory programmatically?

thank you

Anatoly
 
Anatoly,

Usually the application is stored in Program Files\YourAppName. I also use
that location for data. If you choose to use the app directory for data, you
can determine the directory programmatically from
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetN
ame().CodeBase)
 
Flynn said:
I am using a class in C# to store application settings,
but I'd like to be able to write those settings to a
file. According to a book I have for the desktop
Framework, I need to Serialize the class, then use the
GetObjectData method of the ISerializable interface to
define the properties I want to be serialized.

Only problem is, System.Runtime.Serialization doesn't
seem to be supported by the Compact Framework. Any ideas
how I can write the Object properties I want to a
text/XML/binary file? Thanks =)


Hi Flynn,

you may like to take a look at our object database
engine for CF, db4o.

db4o uses reflection to analyse objects in real time.
It's just one call to store any object.

Speed, file size and memory consumption should beat
any XML solution dramatically.

Kind regards,
Carl
 
Hi, Carl

Interesting product. =)

I just ended up dumping the properties directly to the file by using
StreamWriter.WriteLine(instance.property). Quick, painless, and kind of
surprised I didn't think of it in the first place... =D Of course,
everything has to be converted to/from String on use, but that takes a line
of code per property. In fact, I just do the conversion when I read the data
back into the object.

Flynn
 
Flynn said:
Interesting product. =)
Thanks!


I just ended up dumping the properties directly to the file by using
StreamWriter.WriteLine(instance.property). Quick, painless, and kind of
surprised I didn't think of it in the first place... =D Of course,
everything has to be converted to/from String on use, but that takes a line
of code per property. In fact, I just do the conversion when I read the data
back into the object.

Hi Flynn,

if that works out for you, that's great.

Just keep in mind that a fixed format will get you in trouble,
should you want to change it at any time. In case you want to
add new properties your deployed application will have to run
a conversion on your properties file.

Our engine takes care of this issue automatically. You can
simply add fields to classes and continue to use the same
database file.


Your approach will also need a line of code for every single
property. With our engine it would be possible to hold all
the information clean and lean in a class structure. One
line of code would be necessary to store the complete structure:

db.set(structureObject);


One line would read all of it back into an object, no matter
how complex the structure is:

StructureClass structureObject = (StructureClass)db.get(new StructureClass()).next())


All the best for your application!


Kind regards,
Carl
 
Back
Top