{"id":11445,"date":"2016-04-20T09:38:23","date_gmt":"2016-04-20T14:38:23","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=11445"},"modified":"2016-04-20T09:38:23","modified_gmt":"2016-04-20T14:38:23","slug":"c-example-xml-property-file","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=11445","title":{"rendered":"C# Example: XML Property File"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Environment\">Environment<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#XML_Property_File\">XML Property File<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#MyProperties_Class\">MyProperties Class<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Unit_Test\">Unit Test<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Environment\"><h2>Environment<\/h2><\/span>\n<p>* Visual Studio 2013<\/p>\n<span id=\"XML_Property_File\"><h2>XML Property File<\/h2><\/span>\n<p>* Add a new XML property file in the Properties directory:<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<Settings>\r\n  <add key=\"prop1\" value=\"value1\" \/>\r\n  <add key=\"prop2\" value=\"value2\" \/>\r\n<\/Settings>\r\n<\/pre>\n<span id=\"MyProperties_Class\"><h2>MyProperties Class<\/h2><\/span>\n<pre lang=\"csharp\">\r\n    public class MyProperties\r\n    {\r\n        private static readonly ILog log =\r\n        LogManager.GetLogger(typeof(MyProperties));\r\n\r\n        private const string propDir = \"Properties\";\r\n        private const string myprofile = propDir + \"\/fcx.xml\";\r\n        private Dictionary<string, string> myprops = null;\r\n\r\n        \/\/ Singleton\r\n        private static MyProperties instance;\r\n        private MyProperties() { }\r\n        public static MyProperties Instance\r\n        {\r\n            get\r\n            {\r\n                if (instance == null)\r\n                {\r\n                    instance = new MyProperties();\r\n                    instance.LoadProperties();\r\n                }\r\n                return instance;\r\n            }\r\n        }\r\n\r\n        \/\/ Find value from key\r\n        public string FindValue(string key)\r\n        {\r\n            return this.FindValue(key, this.myprops);\r\n        }\r\n\r\n        \/\/ =================================================== Common\r\n        private string FindValue(string key, Dictionary<string, string> props)\r\n        {\r\n            foreach (string k in props.Keys)\r\n            {\r\n                if (key.Equals(k, StringComparison.InvariantCultureIgnoreCase))\r\n                {\r\n                    return props[key];\r\n                }\r\n            }\r\n            return null;\r\n        }\r\n\r\n        private void LoadProperties()\r\n        {\r\n            \/\/ Load once\r\n            if (myprops == null)\r\n            {\r\n                myprops = new Dictionary<string, string>();\r\n                this.LoadProperties(myprofile, myprops);\r\n            }\r\n        }\r\n\r\n        private void LoadProperties(string file, Dictionary<string, string> props)\r\n        {\r\n            if (!File.Exists(file))\r\n            {\r\n                log.ErrorFormat(\"Property file not found: {0}\", myprofile);\r\n                Environment.Exit(0);\r\n            }\r\n\r\n            XmlDocument doc = new XmlDocument();\r\n            doc.Load(file);\r\n            foreach (XmlNode c in doc.ChildNodes)\r\n            {\r\n                if (c.Name.Equals(\"Settings\"))\r\n                {\r\n                    foreach (XmlNode n in c.ChildNodes)\r\n                    {\r\n                        if (n.Name.Equals(\"add\", \r\n                            StringComparison.InvariantCultureIgnoreCase))\r\n                        {\r\n                            props.Add(\r\n                                n.Attributes[\"key\"].Value,\r\n                                n.Attributes[\"value\"].Value);\r\n                        }\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<span id=\"Unit_Test\"><h2>Unit Test<\/h2><\/span>\n<pre lang=\"csharp\">\r\n    [TestClass]\r\n    public class UnitTest1\r\n    {\r\n        private static readonly ILog log = \r\n            LogManager.GetLogger(typeof(UnitTest1));\r\n        \r\n        [TestMethod]\r\n        public void TestMethod1()\r\n        {\r\n            string k = \"prop1\";\r\n            string v = MyProperties.Instance.FindValue(k);\r\n            log.InfoFormat(\"Found value {0} for key {1}.\", v, k);\r\n        }\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Environment * Visual Studio 2013 XML Property File * Add a new XML property file in the Properties directory: MyProperties Class public class MyProperties { private static readonly ILog log = LogManager.GetLogger(typeof(MyProperties)); private const string propDir = &#8220;Properties&#8221;; private const &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=11445\">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,335,674],"class_list":["post-11445","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-properties","tag-xml"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2YB","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11445","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=11445"}],"version-history":[{"count":2,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11445\/revisions"}],"predecessor-version":[{"id":11447,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11445\/revisions\/11447"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}