Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Iso3166.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Xml;
6using Waher.Events;
8
10{
14 public static class Iso3166
15 {
16 private static readonly SortedDictionary<string, string> countryByCode = new SortedDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
17 private static readonly SortedDictionary<string, string> codeByCountry = new SortedDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
18
19 internal static void Load()
20 {
21 try
22 {
23 XmlDocument Doc = new XmlDocument();
24 Doc.Load(Path.Combine(Gateway.AppDataFolder, "ISO-3166-1.xml"));
25
26 foreach (XmlNode N in Doc.DocumentElement.ChildNodes)
27 {
28 if (N is XmlElement E && E.LocalName == "Entry")
29 {
30 string Code = XML.Attribute(E, "code");
31 string Country = XML.Attribute(E, "country");
32
33 if (!string.IsNullOrEmpty(Code) && !string.IsNullOrEmpty(Country))
34 {
35 countryByCode[Code] = Country;
36 codeByCountry[Country] = Code;
37 }
38 }
39 }
40 }
41 catch (Exception ex)
42 {
43 Log.Exception(ex);
44 }
45 }
46
50 public static string[] Codes
51 {
52 get
53 {
54 string[] Result = new string[countryByCode.Count];
55 countryByCode.Keys.CopyTo(Result, 0);
56 return Result;
57 }
58 }
59
63 public static string[] Countries
64 {
65 get
66 {
67 string[] Result = new string[codeByCountry.Count];
68 codeByCountry.Keys.CopyTo(Result, 0);
69 return Result;
70 }
71 }
72
80 public static bool TryGetCountry(string Code, out string Country)
81 {
82 return countryByCode.TryGetValue(Code, out Country);
83 }
84
92 public static bool TryGetCode(string Country, out string Code)
93 {
94 return codeByCountry.TryGetValue(Country, out Code);
95 }
96 }
97}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static string AppDataFolder
Application data folder.
Definition: Gateway.cs:2369