tags:

views:

186

answers:

3

I have an object (User) which is not marked as [Serializable()].

I need to convert the entire object (including child objects) to string.

This is an actual need to convert the object from a third party tool response which is not marked as [Serializable()].

How can i convert an entire C# object to string/xml of the above scenario?

+4  A: 

The XmlSerializer does not need the Serializable attribute, but it can only serialize public members.

Best Regards
Oliver Hanappi


Edit: You can create your own adapter class, which implements the IXmlSerializable interface and represents one User object which your adapter gets when constructed.

Oliver Hanappi
How can i get the Non-public members to string/xml? The third party tool has a set of methods from which i can get the values. I need to get the values from each of the methods or otherwise the non-public members value
Prasad
You could implement the IXmlSerializable interface on an adapter class (see edit).
Oliver Hanappi
+1  A: 

If JSON satisfies your needs, you can try JsonExSerializer as it does not need any attributes to decorate targeted objects.

DrJokepu
A: 

You could use reflection to find all of the members that you are interested in e.g. public properties and/or private fields and then construct an xml document as you go.

That way would could keep the code generic and as custom as you like. :)

However, remember that reflection can be a very slow process at runtime. :(

Penfold