{"id":6019,"date":"2014-04-11T23:17:28","date_gmt":"2014-04-11T23:17:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/xml-serialization-how-to-exclude-objects-at-runtime-collection-of-common-programming-errors\/"},"modified":"2014-04-11T23:17:28","modified_gmt":"2014-04-11T23:17:28","slug":"xml-serialization-how-to-exclude-objects-at-runtime-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/xml-serialization-how-to-exclude-objects-at-runtime-collection-of-common-programming-errors\/","title":{"rendered":"Xml serialization, how to exclude objects at runtime-Collection of common programming errors"},"content":{"rendered":"<p>Hello,<\/p>\n<p>This is just an\u00a0idea, theory if you like, that might be a solution but I haven&#8217;t put this into practise so it may not work. The way you control whether an object is serialised is through attributes, the XmlIgnore attribute, however the problem, as you may already know, is you cannot base that attribute on the value of a variable. The attribute doesn&#8217;t accept a true \/ false value and even if it did it wouldn&#8217;t work off the value of a variable. There are attributes that do allow you to use the value of a variable (DebuggerDisplayAttribute allows it), so maybe one idea could be to develop\u00a0a custom XmlIgnore attribute, maybe that would work.<\/p>\n<p>The idea I had though was to provide a set of XmlAttributeOverrides to the serialiser. This object allows you to change the attributes specified in the class being serialised, so for example you can remove a XmlIgnore attribute with a XmlElement attribute or, more importantly,\u00a0vice versa. Maybe this is the solution, you could base what attributes are overriden based on the value of CanSerialise, so for example by default the object being serialised has an attribute of XmlElement, somewhere in your code you check the value of CanSerialise and if false you override the XmlElement with an XmlIgnore element and hence it doesn&#8217;t get serialised.<\/p>\n<p>Thats just an idea off the top of my head\u00a0so maybe someone has an easier solution, or even some code that demonstrates using the XmlAttributeOverrides class. Anyway I hope this helps and at least gives you a couple of approaches to achieving whats needed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, This is just an\u00a0idea, theory if you like, that might be a solution but I haven&#8217;t put this into practise so it may not work. The way you control whether an object is serialised is through attributes, the XmlIgnore attribute, however the problem, as you may already know, is you cannot base that attribute [&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-6019","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6019","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=6019"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6019\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}