Serialisation one little piece after the other?

H

Helmut Giese

Hello out there,
I have a class which is almost serializable - it contains one element
which isn't. Currently I have this:
[Serializable]
class MetaControl {
[XmlIgnore]
Control ctrl; // this is the problem child
int foo;
<lots more variables>
}

Oh, and I have a 2 dimensional array of these:
MetaControl[,] metaControl;

What would be a way to get this array serialized/deserialized?

Idea: Is there a way to perform serialisation "piecemeal"?
- Start: Let the serialiser write the XML header
- Intro: Serialize numRows & numColumns
- Loop: Feed it
-- one MetaControl followed by
-- a custom serialisation of its Control's properties
- End: Let the serialiser terminate the XML

I am asking, because all examples which I found just call
serializer.Serialize(outputStream, <...>)
where '<...>' is the total to be sent out - be it one object, a list
or some other kind of container.

At the worst I could of course create a DOM tree and write it from
there but hopefully there is a nicer solution.

Any idea on how to solve this will be greatly appreciated.
Best regards
Helmut Giese
 
A

Alberto Poblacion

Helmut Giese said:
Idea: Is there a way to perform serialisation "piecemeal"?

If you are using XML serialization (in which case your [Serializable]
attribute is superfluous): Modify your class so that it implements the
IXmlSerializable interface. Then implement the methods in this interface,
which include a method called "WriteXml" where you can do your own custom
serialization.

If you are using Binary or Soap serialization (in which case your
[XmlIgnore] attribute is superfluous): Modify your class to implement
ISerializable, and then perform your custom serialization inside the
GetObjectData method.
 
H

Helmut Giese

Hi Alberto,
you're way ahead of me :)

Your words sound like this is just what I was looking for - but I
don't really understand them (this being my first project using .NET
so I am still a bit overwhelmed).

Maybe you have a link or two to some short examples which could serve
as illustration? This would be great.

In any case many thanks for the pointers you already provided and best
regards
Helmut Giese
Helmut Giese said:
Idea: Is there a way to perform serialisation "piecemeal"?

If you are using XML serialization (in which case your [Serializable]
attribute is superfluous): Modify your class so that it implements the
IXmlSerializable interface. Then implement the methods in this interface,
which include a method called "WriteXml" where you can do your own custom
serialization.

If you are using Binary or Soap serialization (in which case your
[XmlIgnore] attribute is superfluous): Modify your class to implement
ISerializable, and then perform your custom serialization inside the
GetObjectData method.
 
A

Alberto Poblacion

Helmut Giese said:
you're way ahead of me :)

Your words sound like this is just what I was looking for - but I
don't really understand them (this being my first project using .NET
so I am still a bit overwhelmed).

Maybe you have a link or two to some short examples which could serve
as illustration? This would be great.

If you are using the XML Serializer, then you can do this:

class MetaControl : IXmlSerializable
{
[XmlIgnore]
Control ctrl; // this is the problem child
int foo;
<lots more variables>

public void WriteXml (XmlWriter writer)
{
writer.WriteElementString("foo", foo.ToString());
/* write other variables here */
}

public void ReadXml (XmlReader reader)
{
/* deserialize here */
}
}

Link to MSDN:
http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

If you are using the binary serializer:

[Serializable]
class MetaControl : ISerializable
{
Control ctrl; // this is the problem child
int foo;
<lots more variables>

public MetaControl () {} //Default constructor
public MetaControl (SerializationInfo si, StreamingContext context)
{
//Specialized constructor for deserializing.
foo = si.GetInt32("foo");
/* other fields here */
}
public void GetObjectData(SerializationInfo si,StreamingContext context)
{
si.AddValue("foo", foo);
/* other fields here */
Type t = this.GetType();
si.AddValue("TypeObj", t);
}

Link:
http://msdn.microsoft.com/en-us/lib...erialization.iserializable.getobjectdata.aspx
 
H

Helmut Giese

Whow Alberto,
that was really kind - and very helpful. I am well on my way
serializing the whole world - deserializing turned out to be a bit
more involved, but it looks like I'll manage this, too.

Many thanks and best regards
Helmut Giese
Helmut Giese said:
you're way ahead of me :)

Your words sound like this is just what I was looking for - but I
don't really understand them (this being my first project using .NET
so I am still a bit overwhelmed).

Maybe you have a link or two to some short examples which could serve
as illustration? This would be great.

If you are using the XML Serializer, then you can do this:

class MetaControl : IXmlSerializable
{
[XmlIgnore]
Control ctrl; // this is the problem child
int foo;
<lots more variables>

public void WriteXml (XmlWriter writer)
{
writer.WriteElementString("foo", foo.ToString());
/* write other variables here */
}

public void ReadXml (XmlReader reader)
{
/* deserialize here */
}
}

Link to MSDN:
http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlserializable.aspx

If you are using the binary serializer:

[Serializable]
class MetaControl : ISerializable
{
Control ctrl; // this is the problem child
int foo;
<lots more variables>

public MetaControl () {} //Default constructor
public MetaControl (SerializationInfo si, StreamingContext context)
{
//Specialized constructor for deserializing.
foo = si.GetInt32("foo");
/* other fields here */
}
public void GetObjectData(SerializationInfo si,StreamingContext context)
{
si.AddValue("foo", foo);
/* other fields here */
Type t = this.GetType();
si.AddValue("TypeObj", t);
}

Link:
http://msdn.microsoft.com/en-us/lib...erialization.iserializable.getobjectdata.aspx
 

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