{"id":4998,"date":"2014-03-30T17:41:45","date_gmt":"2014-03-30T17:41:45","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-serialize-internal-classes-using-xmlserializer-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:41:45","modified_gmt":"2014-03-30T17:41:45","slug":"how-can-i-serialize-internal-classes-using-xmlserializer-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-can-i-serialize-internal-classes-using-xmlserializer-collection-of-common-programming-errors\/","title":{"rendered":"How can I serialize internal classes using XmlSerializer?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m building a library to interface with a third party. Communication is through XML and HTTP Posts. That&#8217;s working.<\/p>\n<p>But, whatever code uses the library does not need to be aware of the internal classes. My internal objects are serialized to XML using this method:<\/p>\n<pre><code>internal static string SerializeXML(Object obj)\n{\n    XmlSerializer serializer = new XmlSerializer(obj.GetType(), \"some.domain\");\n\n    \/\/settings\n    XmlWriterSettings settings = new XmlWriterSettings();\n    settings.Indent = true;\n    settings.OmitXmlDeclaration = true;\n\n    using (StringWriter stream = new StringWriter())\n    {\n        using (XmlWriter writer = XmlWriter.Create(stream, settings))\n        {\n            serializer.Serialize(writer, obj);\n        }\n        return stream.ToString();\n    }\n}\n<\/code><\/pre>\n<p>However, when I change my classes&#8217; access modifier to <code>internal<\/code>, I get an exception at <strong>runtime<\/strong>:<\/p>\n<blockquote>\n<p>[System.InvalidOperationException] = {&#8220;MyNamespace.MyClass is inaccessible due to its protection level. Only public types can be processed.&#8221;}<\/p>\n<\/blockquote>\n<p>That exception happens in the first line of the code above.<\/p>\n<p>I would like my library&#8217;s classes not to be public because I do not want to expose them. Can I do that? How can I make internal types serializable, using my generic serializer? What am I doing wrong?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m building a library to interface with a third party. Communication is through XML and HTTP Posts. That&#8217;s working. But, whatever code uses the library does not need to be aware of the internal classes. My internal objects are serialized to XML using this method: internal static string SerializeXML(Object obj) { XmlSerializer serializer = new [&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-4998","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4998","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=4998"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4998\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}