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

Serialization and ISite

P: n/a
Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?

Apr 4 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
If you implement the ISerializable interface, this is not going to give
you the same semantics as using IXmlSerializer.

When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the object.

When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on properties
doesn't matter, since the serialization engine is always going to dig into
the type and get the fields.

However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.

I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that your
components site is either serializable, or don't serialize the reference to
the site in your custom ISerializable implementation.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mi*****@gmail.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?

Apr 4 '07 #2

P: n/a
or don't serialize the reference to
the site in your custom ISerializable implementation.
Yes, this is the thing that confuses me. I didn't serialize anything
else bat one strings (just for test)
[Serializable]
public class PrintLibDoc : System.Drawing.Printing.PrintDocument ,
ISerializable
{

public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("DocName", this.DocumentName);
}
....
}

....
XmlSerializer serializer = new XmlSerializer(typeof(PrintLibDoc));
....
That's the reason why I implemented ISerializable. To prevent
serialization of members that are not serializable. PrintLibDoc shold
only serialize one string. I know that there are other ways to solve
this but this is confusing me.


On Apr 4, 5:09 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
If you implement the ISerializable interface, this is not going to give
you the same semantics as using IXmlSerializer.

When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the object.

When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on properties
doesn't matter, since the serialization engine is always going to dig into
the type and get the fields.

However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.

I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that your
components site is either serializable, or don't serialize the reference to
the site in your custom ISerializable implementation.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<mija...@gmail.comwrote in message

news:11**********************@y66g2000hsf.googlegr oups.com...
Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?- Hide quoted text -

- Show quoted text -

Apr 5 '07 #3

P: n/a
Well, that's the problem. Why are you using the XmlSerializer class
when the ISerializable interface is for Binary and Soap serialization?

If you want plain old xml for serialization, you need to implement the
IXmlSerializable interface, as THAT will give you control over the
serialization details.

If you are ok with Binary/SOAP serialization, then you can use the
BinaryFormatter or SoapFormatter classes to drive the serialization in the
System.Runtime.Serialization.Formatters.Binary and
System.Runtime.Serialization.Formatters.Soap namespaces, respectively.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<mi*****@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
>
>or don't serialize the reference to
the site in your custom ISerializable implementation.

Yes, this is the thing that confuses me. I didn't serialize anything
else bat one strings (just for test)
[Serializable]
public class PrintLibDoc : System.Drawing.Printing.PrintDocument ,
ISerializable
{

public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("DocName", this.DocumentName);
}
...
}

...
XmlSerializer serializer = new XmlSerializer(typeof(PrintLibDoc));
...
That's the reason why I implemented ISerializable. To prevent
serialization of members that are not serializable. PrintLibDoc shold
only serialize one string. I know that there are other ways to solve
this but this is confusing me.


On Apr 4, 5:09 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> If you implement the ISerializable interface, this is not going to
give
you the same semantics as using IXmlSerializer.

When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties
exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the
object.

When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on
properties
doesn't matter, since the serialization engine is always going to dig
into
the type and get the fields.

However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.

I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that
your
components site is either serializable, or don't serialize the reference
to
the site in your custom ISerializable implementation.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<mija...@gmail.comwrote in message

news:11**********************@y66g2000hsf.googleg roups.com...
Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?- Hide quoted
text -

- Show quoted text -


Apr 5 '07 #4

P: n/a
It's clear now.
Thanks a lot

On Apr 5, 6:16 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Well, that's the problem. Why are you using the XmlSerializer class
when the ISerializable interface is for Binary and Soap serialization?

If you want plain old xml for serialization, you need to implement the
IXmlSerializable interface, as THAT will give you control over the
serialization details.

If you are ok with Binary/SOAP serialization, then you can use the
BinaryFormatter or SoapFormatter classes to drive the serialization in the
System.Runtime.Serialization.Formatters.Binary and
System.Runtime.Serialization.Formatters.Soap namespaces, respectively.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

<mija...@gmail.comwrote in message

news:11*********************@d57g2000hsg.googlegro ups.com...


or don't serialize the reference to
the site in your custom ISerializable implementation.
Yes, this is the thing that confuses me. I didn't serialize anything
else bat one strings (just for test)
[Serializable]
public class PrintLibDoc : System.Drawing.Printing.PrintDocument ,
ISerializable
{
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("DocName", this.DocumentName);
}
...
}
...
XmlSerializer serializer = new XmlSerializer(typeof(PrintLibDoc));
...
That's the reason why I implemented ISerializable. To prevent
serialization of members that are not serializable. PrintLibDoc shold
only serialize one string. I know that there are other ways to solve
this but this is confusing me.
On Apr 4, 5:09 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
If you implement the ISerializable interface, this is not going to
give
you the same semantics as using IXmlSerializer.
When using IXmlSerializer, it is going to serialize all public
properties. The problem with this is that if one of the properties
exposes
an interface type, the Xml serialization engine doesn't know what the
appropriate implementation type should be (since it can be anything that
implements it) and won't know what to create when re-hydrating the
object.
When you use the serialization engine, you are serializing the fields
that are in the object. Because of that, exposing interfaces on
properties
doesn't matter, since the serialization engine is always going to dig
into
the type and get the fields.
However, you have to make sure that all fields are of types that have
the Serializable attribute applied to them, which is the case here.
I would recommend using the Serialization framework (not the Xml
serialization framework) to serialize these types, and make sure that
your
components site is either serializable, or don't serialize the reference
to
the site in your custom ISerializable implementation.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
<mija...@gmail.comwrote in message
>news:11**********************@y66g2000hsf.googleg roups.com...
Hi,
I have inherited my class from System.Drawing.Printing.PrintDocument
and I wish to serialize this object using XmlSerializer. And I get
exception "There was an error reflecting type ...". If I look at
innerException it says: "Cannot serialize member
'System.ComponentModel.Component.Site' of type
'System.ComponentModel.ISite'.
OK it is problem to serialize all data so I'll implement my
Serialization. I implemented ISerializable interface, but the problem
is the same. Shouldn't implementation of ISerializable override
default serialization and allow me to serialize MyPrintDocument class?
And have you any suggestion how can I solve this problem?- Hide quoted
text -
- Show quoted text -- Hide quoted text -

- Show quoted text -

Apr 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.