{"id":5971,"date":"2014-04-11T06:54:12","date_gmt":"2014-04-11T06:54:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/how-to-serialize-a-system-type-object-with-xmlserializer-collection-of-common-programming-errors\/"},"modified":"2014-04-11T06:54:12","modified_gmt":"2014-04-11T06:54:12","slug":"how-to-serialize-a-system-type-object-with-xmlserializer-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/how-to-serialize-a-system-type-object-with-xmlserializer-collection-of-common-programming-errors\/","title":{"rendered":"how to serialize a System.Type object with XmlSerializer?-Collection of common programming errors"},"content":{"rendered":"<p>Consider this type:<\/p>\n<pre><code>  [DataContract]\n  public class EntityId\n  {\n    [DataMember(Order = 1)]\n    public string IdAsString { get; set; }\n    [DataMember(Order = 2)]\n    public Type Type { get; set; }\n  }\n<\/code><\/pre>\n<p>I created an Xml serializer assembly for it. However, trying to serialize it yeilds an exception:<\/p>\n<pre><code>System.InvalidOperationException occurred\n  Message=The type NC.DTO.FlowFolder was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.\n  Source=NC.DTO.XmlSerializers\n  StackTrace:\n       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(String n, String ns, Type o, Boolean isNullable, Boolean needType)\n  InnerException: \n<\/code><\/pre>\n<p>Where <strong>NC.DTO.FlowFolder<\/strong> is actually the string representation of <code>typeof(NC.DTO.FlowFolder)<\/code> &#8211; the type <code>NC.DTO.FlowFolder<\/code> is known to the Xml serializer, what it seems to fail to serialize is the <code>Type<\/code> object itself.<\/p>\n<p>The reflector reveals the failing method contents:<\/p>\n<pre><code>private void Write3_Type(string n, string ns, Type o, bool isNullable, bool needType)\n{\n    if (o == null)\n    {\n        if (isNullable)\n        {\n            base.WriteNullTagLiteral(n, ns);\n        }\n    }\n    else if (!needType &amp;&amp; (o.GetType() != typeof(Type)))\n    {\n        throw base.CreateUnknownTypeException(o);\n    }\n}\n<\/code><\/pre>\n<p>Notice the <code>throw<\/code> statement.<\/p>\n<p>The stack trace is:<\/p>\n<pre><code>NC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_Type(string n, string ns, System.Type o, bool isNullable, bool needType) + 0xda bytes    \nNC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write11_EntityId(string n, string ns, NC.DTO.EntityId o, bool isNullable, bool needType) + 0x2a8 bytes  \nNC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write14_FlowFolder(string n, string ns, NC.DTO.FlowFolder o, bool isNullable, bool needType) + 0x36b bytes  \nNC.DTO.XmlSerializers.dll!Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write42_FlowFolder(object o) + 0xc9 bytes   \n[Native to Managed Transition]  \nSystem.Xml.dll!System.Xml.Serialization.TempAssembly.InvokeWriter(System.Xml.Serialization.XmlMapping mapping, System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 342 + 0xb9 bytes    C#\nSystem.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id) Line 676 + 0xdf bytes   C#\nSystem.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle) Line 646   C#\nSystem.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces) Line 640 C#\nSystem.Xml.dll!System.Xml.Serialization.XmlSerializer.Serialize(System.Xml.XmlWriter xmlWriter, object o) Line 616  C#\n<\/code><\/pre>\n<p>My question is how can I make Xml serializer happy to serialize and deserialize <code>EntityId<\/code> objects?<\/p>\n<p>Thanks.<\/p>\n<p><strong>P.S.<\/strong><\/p>\n<p>If someone knows an alternative Xml serialization library, something that would be to XML serialization as Newtonsoft.Json to JSON serialization &#8211; please please please share.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider this type: [DataContract] public class EntityId { [DataMember(Order = 1)] public string IdAsString { get; set; } [DataMember(Order = 2)] public Type Type { get; set; } } I created an Xml serializer assembly for it. However, trying to serialize it yeilds an exception: System.InvalidOperationException occurred Message=The type NC.DTO.FlowFolder was not expected. Use the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5971","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5971"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5971\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}