Serialising objects to pass over a webservice

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:

CDataTableCollection
CDataTable
CDataRow
CDataElement

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.

Question:

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

Apresto
LVL 23
aprestoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
vo1dCommented:
here is a good example, which shows you how to serialize objects to xml:
http://www.dotnetjohn.com/articles.aspx?articleid=173
0
 
aprestoAuthor Commented:
Great thanks, i'll have a look into it tomoro
0
 
AGBrownCommented:
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:

[Serializable]
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:
[WebMethod]
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:
(1.1) http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemSerializableAttributeClassTopic.asp
(2.0) http://msdn2.microsoft.com/en-us/library/system.serializableattribute.aspx
...or ask and I'll see what I can do.

Andy
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.