开发者

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 dese开发者_JAVA技巧rialize 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜