Serialising objects to pass over a webservice

Posted on 2006-05-08
Last Modified: 2010-04-16
Hi All.

I have a class library which i use as a kind of virtual database so i can store generic info in them,

something like:


the heirarchy is as above also.

What i'm trying to do is convert the above into a base64string (or xml) so that i can pass the result to a webservice, and when its received, reconvert it back to as it was before the base64 conversion.

I have been looking into the system.runtime namespace trying to serialise using binaryFormatter and memory stream but i am gettig errors when i try to convert the top object (CDataTableCollection) and all subsequent objects within it.


How do i convert these objects?
Would i need a method in each object to convert each object individually or can i simply conver the top and the rest is done?

My knowledge of this namespace and serialization in general is quite poor.

Any help in resolving this is greatly appreciated, thanks for your time

Question by:apresto
    LVL 11

    Expert Comment

    here is a good example, which shows you how to serialize objects to xml:
    LVL 23

    Author Comment

    Great thanks, i'll have a look into it tomoro
    LVL 12

    Accepted Solution

    As a note, I personally think that that article makes a bit of a mountain out of a molehill if all you want to do is pass your data objects between clients and web services. It might be useful to save them to disk, but the client to web service communication is a lot simpler than that. Take an example class:
    public class ExampleClass
        public int MyPublicField;
        private int _MyPublicProperty;
        public int MyPublicProperty
            get {return _MyPublicProperty; }
            set {_MyPublicProperty = value;}

    All you have to do to make it serialize is mark it with a Serializable attribute:

    public class ExampleClass
        public int MyPublicField;
        private int _MyPublicProperty;
        public int MyPublicProperty
            get {return _MyPublicProperty; }
            set {_MyPublicProperty = value;}

    So then you can just pass it as a parameter to your webservice:
    public ExampleClass AcceptThenIncrementAndReturnAnExampleClass(ExampleClass receivedClass)
        receivedClass.MyPublicField = receivedClass.MyPublicField + 1;
        receivedClass.MyPublicProperty = receivedClass.MyPublicProperty + 1;
        return receivedClass;

    And that's pretty much all you have to do to in order to pass custom objects between web services and client code. This works with collections, or basic objects, so it will work with your hierarchy.

    The way it works is through the XmlSerializer. When that encounters a class marked with the SerializableAttribute, any public fields, properties and indexers are serialized. The only caveats are that they must all be read and writeable, and that all of the types of these objects must also be serializable. So our serializable class will work with a custom collection which has an indexer:
    public ExampleClass this[int index]
        get {...}
        set {...}

    Making all the fields, properties and indexers is not always practical. You'll notice that the Animal class in that example has read/write properties. If you require more control (such as read-only properties, or secondary properties which just return a part of anther propert), you can use the NonSerializedAttribute to exclude fields, or implement the ISerializable interface as well as [Serializable].

    ISerializable lets you specify exactly which fields are serialized and how, so you can exclude some public fields from serialization (if they are derived from other fields, you would want to do this) or serialize otherwise read-only properties. The interface also requires you to create a private constructor, so make sure you read the documentation before you use it.

    The upshot of all of this is that there is no need to serialise to a string, send the string as a parameter, and unserialize the string to an object; web services take care of the whole thing for you by implementing all the code that you would otherwise have to write by copying that article into your web service and web service proxy layer on the client. If you want to use binary formatters instead, you can adjust that article to use binary formatters, but you'll still need to make sure your classes are serialisable by using the SerializableAttribute and possibly ISerializable.

    For more information, I would read the SerializableAttribute MSDN documentation:
    ...or ask and I'll see what I can do.


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now