{"id":1099,"date":"2012-11-12T15:53:00","date_gmt":"2012-11-12T14:53:00","guid":{"rendered":"http:\/\/doena-journal.net\/en\/?p=1099"},"modified":"2021-09-27T15:44:14","modified_gmt":"2021-09-27T13:44:14","slug":"my-xmlserializer-pattern","status":"publish","type":"post","link":"https:\/\/doena-journal.net\/en\/1099\/my-xmlserializer-pattern","title":{"rendered":"My XmlSerializer Pattern"},"content":{"rendered":"<p>The idea behind the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.xml.serialization.xmlserializer%28v=vs.80%29.aspx\">.NET System.Xml.Serialization.XmlSerializer class<\/a> is that you can easily serialize an XML file into a class tree and vice versa.<\/p>\n<p>If you\u2019re using XML Schema files, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/x6c1kb0s%28v=VS.80%29.aspx\">xsd.exe<\/a> tool can create that class tree for you.<\/p>\n<p>But for simple applications you don\u2019t even need a schema. You simply create a class that has public properties of simple types or complex types that in turn contain simple types. <\/p>\n<p>Then you can throw this object into the XmlSerializer and you\u2019re done. \ud83d\ude42<\/p>\n<p>Here\u2019s my personal XmlSerializer coding pattern for a simple list of settings:<\/p>\n<p><!--more--><\/p>\n<table border=\"1\">\n<tr>\n<td>\n<pre>\r\nusing System;\r\nusing System.IO;\r\nusing System.Text;\r\nusing System.Xml;\r\nusing System.Xml.Serialization;\r\n\r\n[Serializable()]\r\n[XmlRoot()]\r\npublic class Settings\r\n{\r\n   private static XmlSerializer s_Serializer;\r\n\r\n   [XmlArray(\"SettingsList\")]\r\n   [XmlArrayItem(\"Setting\")]\r\n   public Setting[] SettingsList;\r\n\r\n   static Settings()\r\n   {\r\n      \/\/This way is safer than the more obvious\r\n      \/\/s_Serializer = new XmlSerializer(typeof(Settings));\r\n      XmlReflectionImporter importer \r\n         = new XmlReflectionImporter();\r\n      XmlTypeMapping xmlMapping = importer\r\n         .ImportTypeMapping(typeof(Settings), null, null);\r\n      XmlSerializerFactory factory \r\n         = new XmlSerializerFactory();\r\n      s_Serializer = factory.CreateSerializer(xmlMapping);\r\n   }\r\n\r\n   public static Settings Deserialize(String filename)\r\n   {\r\n      using (FileStream stream = new FileStream(filename\r\n         , FileMode.Open, FileAccess.Read, FileShare.Read))\r\n      {\r\n         return (s_Serializer.Deserialize(stream) as Settings);\r\n      }\r\n   }\r\n\r\n   public void Serialize(String filename)\r\n   {\r\n      using (FileStream stream = new FileStream(filename\r\n         , FileMode.Create, FileAccess.Write, FileShare.Read))\r\n      {\r\n         using (XmlTextWriter writer = new XmlTextWriter(stream\r\n            , Encoding.UTF8))\r\n         {\r\n            writer.Formatting = Formatting.Indented;\r\n            s_Serializer.Serialize(writer, this);\r\n         }\r\n      }\r\n   }\r\n}\r\n\r\n[Serializable()]\r\npublic class Setting\r\n{\r\n   public String Key;\r\n\r\n   public String Value;\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p>\nOr maybe you want a simple way to have a crash report when your application crashes for yet unknown reasons:<\/p>\n<table border=\"1\">\n<tr>\n<td>\n<pre>\r\nusing System;\r\nusing System.IO;\r\nusing System.Text;\r\nusing System.Xml;\r\nusing System.Xml.Serialization;\r\n\r\n[Serializable()]\r\n[XmlRoot()]\r\npublic class ExceptionXml\r\n{\r\n   private static XmlSerializer s_Serializer;\r\n\r\n   public String Type;\r\n\r\n   public String Message;\r\n\r\n   public String StackTrace;\r\n\r\n   public ExceptionXml InnerException;\r\n\r\n   static ExceptionXml()\r\n   {\r\n      \/\/This way is safer than the more obvious\r\n      \/\/s_Serializer \r\n      \/\/   = new XmlSerializer(typeof(ExceptionXml));\r\n      XmlReflectionImporter importer \r\n         = new XmlReflectionImporter();\r\n      XmlTypeMapping xmlMapping = importer\r\n         .ImportTypeMapping(typeof(ExceptionXml), null, null);\r\n      XmlSerializerFactory factory \r\n         = new XmlSerializerFactory();\r\n      s_Serializer = factory.CreateSerializer(xmlMapping);\r\n   }\r\n\r\n   public static ExceptionXml Deserialize(String filename)\r\n   {\r\n      using (FileStream stream = new FileStream(filename\r\n         , FileMode.Open, FileAccess.Read, FileShare.Read))\r\n      {\r\n         return (s_Serializer.Deserialize(stream) \r\n            as ExceptionXml);\r\n      }\r\n   }\r\n\r\n   public void Serialize(String filename)\r\n   {\r\n      using (FileStream stream = new FileStream(filename\r\n         , FileMode.Create, FileAccess.Write, FileShare.Read))\r\n      {\r\n         using (XmlTextWriter writer = new XmlTextWriter(stream\r\n            , Encoding.UTF8))\r\n         {\r\n            writer.Formatting = Formatting.Indented;\r\n            s_Serializer.Serialize(writer, this);\r\n         }\r\n      }\r\n   }\r\n\r\n   public ExceptionXml()\r\n   {\r\n   }\r\n\r\n   public ExceptionXml(Exception ex)\r\n   {\r\n      if (ex != null)\r\n      {\r\n         this.Type = ex.GetType().FullName;\r\n         this.Message = ex.Message;\r\n         this.StackTrace = ex.StackTrace;\r\n         if (ex.InnerException != null)\r\n         {\r\n            this.InnerException \r\n               = new ExceptionXml(ex.InnerException);\r\n         }\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>The idea behind the .NET System.Xml.Serialization.XmlSerializer class is that you can easily serialize an XML file into a class tree and vice versa. If you\u2019re&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,870],"tags":[1454,1453,1456,1773],"class_list":["post-1099","post","type-post","status-publish","format-standard","hentry","category-misc","category-software","tag-net","tag-c","tag-serialization","tag-xmlserializer"],"_links":{"self":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/comments?post=1099"}],"version-history":[{"count":21,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts\/1099\/revisions"}],"predecessor-version":[{"id":1192,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/posts\/1099\/revisions\/1192"}],"wp:attachment":[{"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/media?parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/categories?post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doena-journal.net\/en\/wp-json\/wp\/v2\/tags?post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}