UserControl
UserControl

Reputation: 15179

Convert object graph into tree

Say, i have an arbitrary .NET object (well, i can guarantee it has [DataContract] applied) and want to convert it into tree (for example, XML) performing special processing on all its string properties. This looks like a typical serialization task but from what i've learned none of .NET serializers give me control on property content processing (i tried to use DataContractSerializer/XmlSerializer but XmlObjectSerializerWriteContext is internal, thanks to its designers). I don't need to deserialize the tree back, it's only for visual representation.

Do i have any options except manual serialization using reflection (looks like too much work to handle collections, cycles in graphs, etc)?

Update: Ended up with a custom recursive function relying on reflection.

Upvotes: 0

Views: 280

Answers (1)

Jeff
Jeff

Reputation: 36583

Why not perform the string processing on the XML output? Grab all the elements that are strings using XPATH and perform the operation.

You could also implement XmlDictionaryWriter to perform your string processing (you could wrap an XmlDictionaryWriter internally and call methods on that):

http://msdn.microsoft.com/en-us/library/system.xml.xmldictionarywriter.aspx

and use the DataContractSerializer to writer to that:

http://msdn.microsoft.com/en-us/library/ms195072.aspx

Upvotes: 1

Related Questions