{"id":102,"date":"2011-05-22T09:02:12","date_gmt":"2011-05-22T09:02:12","guid":{"rendered":"http:\/\/devnot.wordpress.com\/?p=102"},"modified":"2011-05-22T09:02:12","modified_gmt":"2011-05-22T09:02:12","slug":"serialize-an-object-to-from-xml","status":"publish","type":"post","link":"https:\/\/thomas-jansen.eu\/?p=102","title":{"rendered":"Serialize an object to \/ from XML"},"content":{"rendered":"<p>Serializing\u00a0 allows you to simply store or load an object&#8217;s contents to a XML file.<\/p>\n<p>Classes can be serialized to XML. All public fields will be serialized automatically<\/p>\n<p>Use the <em>[<\/em><em>XmlIgnoreAttribute<\/em><em>]<\/em> to prevent serialization.<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>[XmlIgnoreAttribute]<strong><br \/>\n<\/strong><\/p>\n<p>int notToBeSerialized;<\/p>\n<p><strong>Sample:<\/strong><\/p>\n<p>Save or load an object to an XML file.<\/p>\n<p>These functions can throw exceptions which must be handled !<\/p>\n<pre>class XMLSerializer\n{\n   public static T read&lt;T&gt;(string path)\n   {\n      T data = default(T);\n      System.IO.TextReader reader = new System.IO.StreamReader(path);\n      System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));\n      data = (T)ser.Deserialize(reader);\n      reader.Close();\n      reader.Dispose();\n      return (data);\n   }\n\n   public static void write&lt;T&gt;(string path, string filename, T data)\n   {\n      if (!Directory.Exists(path)) Directory.CreateDirectory(path);\n      path += \"\\\" + filename;\n      System.IO.TextWriter writer = new System.IO.StreamWriter(path);\n      System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));\n      ser.Serialize(writer, data);\n      writer.Close();\n      writer.Dispose();\n      return (true);\n   }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Serializing\u00a0 allows you to simply store or load an object&#8217;s contents to a XML file. Classes can be serialized to XML. All public fields will be serialized automatically Use the [XmlIgnoreAttribute] to prevent serialization. Example [XmlIgnoreAttribute] int notToBeSerialized; Sample: Save or load an object to an XML file. These functions can throw exceptions which must [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,69,90],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-c","tag-save-object","tag-serialize","tag-xml"],"_links":{"self":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=102"}],"version-history":[{"count":0,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/102\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}