{"id":11521,"date":"2016-07-14T11:17:25","date_gmt":"2016-07-14T16:17:25","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=11521"},"modified":"2016-07-14T11:18:08","modified_gmt":"2016-07-14T16:18:08","slug":"c-example-readwrite-xml-file","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=11521","title":{"rendered":"C# Example: Read\/Write XML File"},"content":{"rendered":"<p><div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Read_or_DeSerializing_XML_File\">Read or DeSerializing XML File<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Write_or_Serializing_XML_File\">Write or Serializing XML File<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div><br \/>\n(Serializing\/Deserializing) <\/p>\n<span id=\"Read_or_DeSerializing_XML_File\"><h2>Read or DeSerializing XML File<\/h2><\/span>\n<pre lang=\"csharp\">\r\n        \/\/\/ <summary>\r\n        \/\/\/ Reads XML config file.\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <typeparam name=\"T\">Class to read into.<\/typeparam>\r\n        \/\/\/ <param name=\"xmlFile\">XML file location.<\/param>\r\n        \/\/\/ <param name=\"type\">Class type to read from.<\/param>\r\n        \/\/\/ <returns><\/returns>\r\n        public static T ReadXMLFile<T>(string xmlFile, Type type)\r\n        {\r\n            if (!File.Exists(xmlFile))\r\n            {\r\n                log.FatalFormat(\"XML config file '{0}' not found!\", xmlFile);\r\n                return default(T);\r\n            }\r\n\r\n            log.InfoFormat(\"Found XML config file '{0}'. Deserializing...\", xmlFile);\r\n            var reader = new System.Xml.Serialization.XmlSerializer(type);\r\n            var xmlfile = new System.IO.StreamReader(xmlFile);\r\n            return (T)reader.Deserialize(xmlfile);\r\n        }\r\n<\/pre>\n<p>* Example usage:<\/p>\n<pre lang=\"csharp\">\r\n        [TestMethod]\r\n        public void TestReadXMLFile()\r\n        {\r\n            UserMenu um = Util.ReadXMLFile<UserMenu>(\r\n                Properties.MyMenuXMLFile, typeof(UserMenu));\r\n            Assert.IsNotNull(um);\r\n            log.InfoFormat(\"Got MyMenu: {0}.\", um.MenuName);\r\n        }\r\n<\/pre>\n<span id=\"Write_or_Serializing_XML_File\"><h2>Write or Serializing XML File<\/h2><\/span>\n<pre lang=\"csharp\">\r\n        \/\/\/ <summary>\r\n        \/\/\/ Generate a dummy menu XML file.\r\n        \/\/\/ <\/summary>\r\n        \/\/\/ <param name=\"xmlFile\">Output XML file name.<\/param>\r\n        \/\/\/ <param name=\"type\">Coresponding class.<\/param>\r\n        \/\/\/ <param name=\"force\">If true, overwrite existing XML file.<\/param>\r\n        public static void GenerateDummyXMLFile(string xmlFile, Type type, bool force)\r\n        {\r\n            if (File.Exists(xmlFile) && !force)\r\n            {\r\n                log.WarnFormat(\r\n                    \"XML config file '{0}' exists and we're not forcing the serialization.\", xmlFile);\r\n                return;\r\n            }\r\n\r\n            log.InfoFormat(\"XML config file '{0}' not found. Serializing...\", xmlFile);\r\n            var writer = new System.Xml.Serialization.XmlSerializer(type);\r\n            var xmlfile = new System.IO.StreamWriter(xmlFile);\r\n            writer.Serialize(xmlfile, Properties.DummyMyMenu);\r\n            xmlfile.Close();\r\n            log.InfoFormat(\"Serialized to xml file '{0}'.\", xmlFile);\r\n        }\r\n\r\n<\/pre>\n<p>* Example usage:<\/p>\n<pre lang=\"csharp\">\r\n        [TestMethod]\r\n        public void TestGenerateDummyXML()\r\n        {\r\n            Util.GenerateDummyXMLFile(Properties.MyMenuXMLFile, typeof(UserMenu), false);\r\n        }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>(Serializing\/Deserializing) Read or DeSerializing XML File \/\/\/ \/\/\/ Reads XML config file. \/\/\/ \/\/\/ Class to read into. \/\/\/ XML file location. \/\/\/ Class type to read from. \/\/\/ public static T ReadXMLFile(string xmlFile, Type type) { if (!File.Exists(xmlFile)) { &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=11521\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[556,697,696],"class_list":["post-11521","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-deserialize","tag-serialize"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2ZP","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11521"}],"version-history":[{"count":1,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11521\/revisions"}],"predecessor-version":[{"id":11522,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11521\/revisions\/11522"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}