> There seems to be (at least) two namespaces dealing with
> serialization.
>
> System.Xml.Serialization and
This namespace contains the types for doing Xml serialization.
> System.Runtime.Serialization
This namespace, and its sub-namespaces, contains the types for doing binary
and SOAP Serialization. Please note that dispite the name, SOAP Serialization
is not used for web services. Web services uses Xml Serialization
> When would I use one namespace and when would I use the other?
Binary serialization is more efficent than Xml serialization in terms of
size and performance, Xml serialization is more interoperable than binary
serialization. Soap Serialization is seldom used and has some issues.
> Ultimately, I want to serialize a complex datatype class which itself,
> has complex datatype fields. Additionally the class has two container
> fields which inherit from a class which is not marked with the
> "Serializeable" attribute.
>
>I want to marshall the data in a language independent way.
To use serialization all the types your serializing must be serializable,
eg. have the [Serializable] attribute. In your scenario I reckon Xml Serialization
is the way to go. Below is an example showing how to serialize and deserialize
a class using the XmlSerializer.
MyClass c=new MyClass();
MemoryStream s=new MemoryStream();
XmlSerializer xs=new XmlSerializer(typeof(MyClass));
// Serialize to XML
xs.Serialize(s,c);
s.Position=0;
// Deserialize from XML
MyClass c2=(MyClass) xs.Deserialize(s);
Regards,
Anders Norås
http://dotnetjunkies.com/weblog/anoras
Karl - 01 Dec 2005 17:16 GMT
Thank you Anders! You have no idea how long I've been trying to get a
handle on this. I've had a similar question posted on another Framwork
newsgroup since mid Nov. and there hasn't been one reply.
Your sample code was very useful. I have just one more question.
If that code were in a WebMethod, what should be the data type that the
method returns? I'm assuming that the return type is an array of bytes
and that I return:
s.ToArray
Correct?
>> There seems to be (at least) two namespaces dealing with
>> serialization.
[quoted text clipped - 41 lines]
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras/
Anders Norås - 01 Dec 2005 20:40 GMT
> If that code were in a WebMethod, what should be the data type that
> the method returns? I'm assuming that the return type is an array of
> bytes and that I return:
Actually the ASP.NET does the serialization for you. Your WebMethod can return
any serializable object. When invoked as a web service, ASP.NET will serialize
your return value (using the XmlSerializer) and embed the return value as
a response in a SOAP enveloped which is returned to the client. The web service
proxy in the client uses will deserialize the response to a proxy representation
of the object you returned from your web method.
In essence, you don't have to serialize and deserialize the objects yourself,
the framework does this for you.
Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Karl - 02 Dec 2005 12:16 GMT
Thanks Anders.
>> Actually the ASP.NET does the serialization for you. Your WebMethod can
>> return any serializable object.
I understand this and have successfully implented webservices this way.
However, this is the issue (I'm using Delphi, btw), Delphi supports
subranges of enumerated types. The serialization that I get when I
return from the function the object breaks the enumeration due to the
fact that the frameworks adds metadata to the subrange enumeration.
Additionally, the webservice's return class has various length fixed
length strings.
Finally, there is also a field (of the class) which is of a container
class type which is not marked as serializable.
I was under the impression that I can deal with these 3 issues via
attributes and custome code in my webmethod which relies on XmlSerializer.
Am I on the wrong track?
>> If that code were in a WebMethod, what should be the data type that
>> the method returns? I'm assuming that the return type is an array of
[quoted text clipped - 12 lines]
> Anders Norås
> http://dotnetjunkies.com/weblog/anoras/