{"id":4948,"date":"2014-03-30T17:11:46","date_gmt":"2014-03-30T17:11:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-exclude-class-fields-from-serialization-by-xmlelement-name-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:11:46","modified_gmt":"2014-03-30T17:11:46","slug":"how-to-exclude-class-fields-from-serialization-by-xmlelement-name-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-exclude-class-fields-from-serialization-by-xmlelement-name-collection-of-common-programming-errors\/","title":{"rendered":"How to exclude class fields from serialization by XmlElement name?-Collection of common programming errors"},"content":{"rendered":"<p>Have you encountered the <code>[XmlIgnore]<\/code> attribute? Adding it to members of your class will exclude them from serialization.<\/p>\n<p>So, for example, you can replace <code>[XmlElement(\"Surname\")]<\/code> with <code>[XmlIgnore]<\/code>, and then your serialized agent will look like this:<\/p>\n<pre><code>\n    John Doe\n\n<\/code><\/pre>\n<p>Alternately, if all you really want is just <code>John Doe<\/code>, you could write a wrapper class:<\/p>\n<pre><code>[XmlRoot(\"Name\")]\npublic class NameElement\n{\n    [XmlText]\n    public string Name { get; set; }\n}\n<\/code><\/pre>\n<p><strong>* EDIT *<\/strong><\/p>\n<p>While it&#8217;s possible to generate such wrappers at runtime, it&#8217;s difficult, inefficient, and not very practical.<\/p>\n<p>To do so, I guess you could reflectively examine your object and find the properties you want (<code>root.GetType().GetProperties().Where(p =&gt; \/* your logic here *\/)<\/code>), and use System.Reflection.Emit to generate the appropriate class. While possible, it&#8217;s not reasonable &#8211; it&#8217;d be a huge amount of code relative to your actual logic, and you could easily destabilize the runtime and\/or leak memory.<\/p>\n<p>A better way to achieve the dynamicism you want is to forego <code>System.Xml.Serialization<\/code> and use <code>System.Xml.Linq<\/code>. This would require you to write code that built up the xml yourself, but it&#8217;s super easy:<\/p>\n<pre><code>public XElement ConvertToXml(RootClass root)\n{\n    return new XElement(\"Name\", root.Name);\n}\n<\/code><\/pre>\n<p>You can write an XElement to any stream using the <code>element.Save(Stream)<\/code> instance method on XElement.<\/p>\n<p>Read more about Linq to XML at MSDN<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you encountered the [XmlIgnore] attribute? Adding it to members of your class will exclude them from serialization. So, for example, you can replace [XmlElement(&#8220;Surname&#8221;)] with [XmlIgnore], and then your serialized agent will look like this: John Doe Alternately, if all you really want is just John Doe, you could write a wrapper class: [XmlRoot(&#8220;Name&#8221;)] [&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-4948","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4948","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=4948"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4948\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}