1

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?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
You could implement the IXmlSerializable interface on an adapter class (see edit).
1

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

Comments

0

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. :(

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.