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;
3using System.IO;
4using System.Xml;
6using Waher.Events;
10
12{
16 public static class Iso3166
17 {
18 private static readonly SortedDictionary<string, string> countryByCode = new SortedDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
19 private static readonly SortedDictionary<string, string> codeByCountry = new SortedDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
20
21 internal static void Load()
22 {
23 try
24 {
25 if (!string.IsNullOrEmpty(Gateway.AppDataFolder))
26 {
27 XmlDocument Doc = XML.LoadFromFile(Path.Combine(Gateway.AppDataFolder, "ISO-3166-1.xml"));
28
29 foreach (XmlNode N in Doc.DocumentElement.ChildNodes)
30 {
31 if (N is XmlElement E && E.LocalName == "Entry")
32 {
33 string Code = XML.Attribute(E, "code");
34 string Country = XML.Attribute(E, "country");
35
36 if (!string.IsNullOrEmpty(Code) && !string.IsNullOrEmpty(Country))
37 {
38 countryByCode[Code] = Country;
39 codeByCountry[Country] = Code;
40 }
41 }
42 }
43 }
44 }
45 catch (Exception ex)
46 {
47 Log.Exception(ex);
48 }
49 }
50
54 public static string[] Codes
55 {
56 get
57 {
58 string[] Result = new string[countryByCode.Count];
59 countryByCode.Keys.CopyTo(Result, 0);
60 return Result;
61 }
62 }
63
67 public static string[] Countries
68 {
69 get
70 {
71 string[] Result = new string[codeByCountry.Count];
72 codeByCountry.Keys.CopyTo(Result, 0);
73 return Result;
74 }
75 }
76
81 public static Dictionary<string, IElement> GetEntries()
82 {
83 Dictionary<string, IElement> Result = new Dictionary<string, IElement>();
84
85 foreach (KeyValuePair<string, string> P in countryByCode)
86 Result[P.Key] = new StringValue(P.Value);
87
88 return Result;
89 }
90
98 public static bool TryGetCountry(string Code, out string Country)
99 {
100 return countryByCode.TryGetValue(Code, out Country);
101 }
102
110 public static bool TryGetCode(string Country, out string Code)
111 {
112 return codeByCountry.TryGetValue(Country, out Code);
113 }
114 }
115}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
static XmlDocument LoadFromFile(string FileName)
Loads an XML document from a file.
Definition: XML.cs:1808
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
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:1657
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static string AppDataFolder
Application data folder.
Definition: Gateway.cs:3132