Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing a CodeDom as XML

Is there a way to serialize a CodeCompileUnit object as XML.

The problem is that:

XmlSerializer xml = new XmlSerializer( typeof(CodeCompileUnit) );

throws the following exception:

"Cannot serialize member System.CodeDom.CodeObject.UserData of type System.Collections.IDictionary, because it implements IDictionary."

like image 464
Eugeniu Torica Avatar asked May 11 '26 15:05

Eugeniu Torica


2 Answers

XmlSerializer has issues with IDictionary. It is now deprecated in favor of DataContractSerializer which can serialize a CodeCompileUnit instance:

var serializer = new DataContractSerializer(typeof(CodeCompileUnit));
serializer.WriteObject(Console.OpenStandardOutput(), new CodeCompileUnit());
like image 103
Darin Dimitrov Avatar answered May 13 '26 04:05

Darin Dimitrov


This is a limitation of the XML serializer : dictionaries can't be serialized (even though I don't see any good reason why). Actually they can be serialized if they implement IXmlSerializable (which, by the way, is a real pain to implement), but that's not the case for the UserData property... so you're stuck

like image 37
Thomas Levesque Avatar answered May 13 '26 06:05

Thomas Levesque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!