{"id":217,"date":"2009-08-20T12:11:53","date_gmt":"2009-08-20T10:11:53","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=217"},"modified":"2009-08-20T12:11:53","modified_gmt":"2009-08-20T10:11:53","slug":"system-xml-serialization","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=217","title":{"rendered":"System.Xml.Serialization"},"content":{"rendered":"<p>I like XML. I like it a lot. For most smaller applications a database &#8211; even SQLite and the likes &#8211; is complete overkill. Still, you have to save your application data somewhere. Enter XML.<\/p>\n<p>I also like to access my application data in a typesafe manner and I don&#8217;t like to reinvent the wheel (or in this case, the XML serialization). Enter System.Xml.Serialization.<!--more--><\/p>\n<p>The idea is simple and straight forward. You create a class with some properties, you decorate the properties with some attributes, combine the whole thing with the System.Xml.Serialization.XmlSerializer and presto chango: you&#8217;ve got yourself some simple and effective XML serialization. Allow me to demonstrate:<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/\/ XML serializable class\r\n&#x5B;XmlRoot(&quot;myRootNode&quot;)]\r\npublic class TestRoot\r\n{\r\n  &#x5B;XmlAttribute(&quot;myAttribute&quot;)]\r\n  public int MyAttribute { get; set; }\r\n\r\n  &#x5B;XmlElement(&quot;myElement&quot;)]\r\n  public string MyElement { get; set; }\r\n}\r\n<\/pre>\n<p>Now you create a new TestRoot object, set the properties and serialize it as follows.<\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/\/ XML serializable class\r\nXmlSerializer serializer = new XmlSerializer(typeof(TestRoot));\r\nusing (StreamWriter writer = File.CreateText(&quot;test.xml&quot;))\r\n{\r\n  serializer.Serialize(writer, this);\r\n}\r\n<\/pre>\n<p>This will result in an XML file similar to this:<\/p>\n<pre class=\"brush: xml; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n&lt;myRootNode myAttribute=&quot;42&quot;&gt;\r\n  &lt;myElement&gt;my element text&lt;\/myElement&gt;\r\n&lt;\/myRootNode&gt;\r\n<\/pre>\n<p>You can also create more complicated structures, you just need the right attributes. If you want to have an object with an array (unfortunately arrays are the only collections that are allowed) of child elements, there are two different ways of doing this.<\/p>\n<p><strong>Method 1:<\/strong><\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/\/ XML serializable class\r\n&#x5B;XmlRoot(&quot;myRootNode&quot;)]\r\npublic class TestRoot\r\n{\r\n  &#x5B;XmlArray(&quot;myArray&quot;)]\r\n  &#x5B;XmlArrayItem(&quot;myItem&quot;)]\r\n  public string&#x5B;] MyItems { get; set; }\r\n}\r\n<\/pre>\n<pre class=\"brush: xml; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n&lt;myRootNode&gt;\r\n  &lt;myArray&gt;\r\n    &lt;myItem&gt;my item text 1&lt;\/myItem&gt;\r\n    &lt;myItem&gt;my item text 2&lt;\/myItem&gt;\r\n    &lt;myItem&gt;my item text 3&lt;\/myItem&gt;\r\n  &lt;\/myArray&gt;\r\n&lt;\/myRootNode&gt;\r\n<\/pre>\n<p><strong>Method 2:<\/strong><\/p>\n<pre class=\"brush: csharp; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n\/\/ XML serializable class\r\n&#x5B;XmlRoot(&quot;myRootNode&quot;)]\r\npublic class TestRoot\r\n{\r\n  &#x5B;XmlElement(&quot;myItem&quot;)]\r\n  public string&#x5B;] MyItems { get; set; }\r\n}\r\n<\/pre>\n<pre class=\"brush: xml; gutter: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n&lt;myRootNode&gt;\r\n  &lt;myItem&gt;my item text 1&lt;\/myItem&gt;\r\n  &lt;myItem&gt;my item text 2&lt;\/myItem&gt;\r\n  &lt;myItem&gt;my item text 3&lt;\/myItem&gt;\r\n&lt;\/myRootNode&gt;\r\n<\/pre>\n<p>The sky&#8217;s the limit. All this also works for complex elements. See the MSDN documentation for more information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I like XML. I like it a lot. For most smaller applications a database &#8211; even SQLite and the likes &#8211; is complete overkill. Still, you have to save your application data somewhere. Enter XML. I also like to access my application data in a typesafe manner and I don&#8217;t like to reinvent the wheel &hellip; <\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,96],"tags":[],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-executor","4":"has-more-link","5":"post-217","7":"format-standard","8":"category-csharp","9":"category-xml-programming"},"acf":[],"views":1885,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/217"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=217"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}