Getting Country Calling Code in .NET Framework - c#
In my program I need to get currency symbol and country calling code for specific countries.
I can get currency symbol from RegionInfo class
new RegionInfo(abbreviation).CurrencySymbol
But I cannot find a way to get country calling code from .NET Framework.
Years too late for the original poster, but if someone stumbles across this & wants a bit of code to save creating themselves a list of dial codes from Wikipedia, an open source project (of mine) has this C# file:
https://github.com/mcshaz/SimPlanner/blob/master/SP.DTOs/ProcessBreezeRequests/ISO3166.cs
public List<CountryTelCode> TelCodes { get; set; }
//*****************************************************************
public class CountryTelCode
//*****************************************************************
{
public string Pfx { get; set; }
public string Iso { get; set; }
public int Priority { get; set; }
public CountryTelCode(string pfx, string iso, int priority = 0)
{
Pfx = pfx;
Iso = iso;
Priority = priority;
}
}
//-----------------------------------------------------------------
public void InitTelCodes()
//-----------------------------------------------------------------
{
TelCodes = new List<CountryTelCode>
{
new CountryTelCode("+93", "AF"),
new CountryTelCode("+355", "AL"),
new CountryTelCode("+213", "DZ"),
new CountryTelCode("+1-684", "AS"),
new CountryTelCode("+376", "AD"),
new CountryTelCode("+244", "AO"),
new CountryTelCode("+1-264", "AI"),
new CountryTelCode("+672", "AQ"),
new CountryTelCode("+1-268", "AG"),
new CountryTelCode("+54", "AR"),
new CountryTelCode("+374", "AM"),
new CountryTelCode("+297", "AW"),
new CountryTelCode("+61", "AU"),
new CountryTelCode("+43", "AT"),
new CountryTelCode("+994", "AZ"),
new CountryTelCode("+1-242", "BS"),
new CountryTelCode("+973", "BH"),
new CountryTelCode("+880", "BD"),
new CountryTelCode("+1-246", "BB"),
new CountryTelCode("+375", "BY"),
new CountryTelCode("+32", "BE"),
new CountryTelCode("+501", "BZ"),
new CountryTelCode("+229", "BJ"),
new CountryTelCode("+1-441", "BM"),
new CountryTelCode("+975", "BT"),
new CountryTelCode("+591", "BO"),
new CountryTelCode("+387", "BA"),
new CountryTelCode("+267", "BW"),
new CountryTelCode("+55", "BR"),
new CountryTelCode("+246", "IO"),
new CountryTelCode("+1-284", "VG"),
new CountryTelCode("+673", "BN"),
new CountryTelCode("+359", "BG"),
new CountryTelCode("+226", "BF"),
new CountryTelCode("+257", "BI"),
new CountryTelCode("+855", "KH"),
new CountryTelCode("+237", "CM"),
new CountryTelCode("+1", "CA"),
new CountryTelCode("+238", "CV"),
new CountryTelCode("+1-345", "KY"),
new CountryTelCode("+236", "CF"),
new CountryTelCode("+235", "TD"),
new CountryTelCode("+56", "CL"),
new CountryTelCode("+86", "CN"),
new CountryTelCode("+61", "CX"),
new CountryTelCode("+61", "CC"),
new CountryTelCode("+57", "CO"),
new CountryTelCode("+269", "KM"),
new CountryTelCode("+682", "CK"),
new CountryTelCode("+506", "CR"),
new CountryTelCode("+385", "HR"),
new CountryTelCode("+53", "CU"),
new CountryTelCode("+599", "CW"),
new CountryTelCode("+357", "CY"),
new CountryTelCode("+420", "CZ"),
new CountryTelCode("+243", "CD"),
new CountryTelCode("+45", "DK"),
new CountryTelCode("+253", "DJ"),
new CountryTelCode("+1-767", "DM"),
new CountryTelCode("+1-809", "DO"),
new CountryTelCode("+1-829", "DO"),
new CountryTelCode("+1-849", "DO"),
new CountryTelCode("+670", "TL"),
new CountryTelCode("+593", "EC"),
new CountryTelCode("+20", "EG"),
new CountryTelCode("+503", "SV"),
new CountryTelCode("+240", "GQ"),
new CountryTelCode("+291", "ER"),
new CountryTelCode("+372", "EE"),
new CountryTelCode("+251", "ET"),
new CountryTelCode("+500", "FK"),
new CountryTelCode("+298", "FO"),
new CountryTelCode("+679", "FJ"),
new CountryTelCode("+358", "FI"),
new CountryTelCode("+33", "FR"),
new CountryTelCode("+689", "PF"),
new CountryTelCode("+241", "GA"),
new CountryTelCode("+220", "GM"),
new CountryTelCode("+995", "GE"),
new CountryTelCode("+49", "DE"),
new CountryTelCode("+233", "GH"),
new CountryTelCode("+350", "GI"),
new CountryTelCode("+30", "GR"),
new CountryTelCode("+299", "GL"),
new CountryTelCode("+1-473", "GD"),
new CountryTelCode("+1-671", "GU"),
new CountryTelCode("+502", "GT"),
new CountryTelCode("+44-1481", "GG"),
new CountryTelCode("+224", "GN"),
new CountryTelCode("+245", "GW"),
new CountryTelCode("+592", "GY"),
new CountryTelCode("+509", "HT"),
new CountryTelCode("+504", "HN"),
new CountryTelCode("+852", "HK"),
new CountryTelCode("+36", "HU"),
new CountryTelCode("+354", "IS"),
new CountryTelCode("+91", "IN"),
new CountryTelCode("+62", "ID"),
new CountryTelCode("+98", "IR"),
new CountryTelCode("+964", "IQ"),
new CountryTelCode("+353", "IE"),
new CountryTelCode("+44-1624", "IM"),
new CountryTelCode("+972", "IL"),
new CountryTelCode("+39", "IT"),
new CountryTelCode("+225", "CI"),
new CountryTelCode("+1-876", "JM"),
new CountryTelCode("+81", "JP"),
new CountryTelCode("+44-1534", "JE"),
new CountryTelCode("+962", "JO"),
new CountryTelCode("+7", "KZ"),
new CountryTelCode("+254", "KE"),
new CountryTelCode("+686", "KI"),
new CountryTelCode("+383", "XK"),
new CountryTelCode("+965", "KW"),
new CountryTelCode("+996", "KG"),
new CountryTelCode("+856", "LA"),
new CountryTelCode("+371", "LV"),
new CountryTelCode("+961", "LB"),
new CountryTelCode("+266", "LS"),
new CountryTelCode("+231", "LR"),
new CountryTelCode("+218", "LY"),
new CountryTelCode("+423", "LI"),
new CountryTelCode("+370", "LT"),
new CountryTelCode("+352", "LU"),
new CountryTelCode("+853", "MO"),
new CountryTelCode("+389", "MK"),
new CountryTelCode("+261", "MG"),
new CountryTelCode("+265", "MW"),
new CountryTelCode("+60", "MY"),
new CountryTelCode("+960", "MV"),
new CountryTelCode("+223", "ML"),
new CountryTelCode("+356", "MT"),
new CountryTelCode("+692", "MH"),
new CountryTelCode("+222", "MR"),
new CountryTelCode("+230", "MU"),
new CountryTelCode("+262", "YT"),
new CountryTelCode("+52", "MX"),
new CountryTelCode("+691", "FM"),
new CountryTelCode("+373", "MD"),
new CountryTelCode("+377", "MC"),
new CountryTelCode("+976", "MN"),
new CountryTelCode("+382", "ME"),
new CountryTelCode("+1-664", "MS"),
new CountryTelCode("+212", "MA"),
new CountryTelCode("+258", "MZ"),
new CountryTelCode("+95", "MM"),
new CountryTelCode("+264", "NA"),
new CountryTelCode("+674", "NR"),
new CountryTelCode("+977", "NP"),
new CountryTelCode("+31", "NL"),
new CountryTelCode("+599", "AN"),
new CountryTelCode("+687", "NC"),
new CountryTelCode("+64", "NZ"),
new CountryTelCode("+505", "NI"),
new CountryTelCode("+227", "NE"),
new CountryTelCode("+234", "NG"),
new CountryTelCode("+683", "NU"),
new CountryTelCode("+850", "KP"),
new CountryTelCode("+1-670", "MP"),
new CountryTelCode("+47", "NO"),
new CountryTelCode("+968", "OM"),
new CountryTelCode("+92", "PK"),
new CountryTelCode("+680", "PW"),
new CountryTelCode("+970", "PS"),
new CountryTelCode("+507", "PA"),
new CountryTelCode("+675", "PG"),
new CountryTelCode("+595", "PY"),
new CountryTelCode("+51", "PE"),
new CountryTelCode("+63", "PH"),
new CountryTelCode("+64", "PN"),
new CountryTelCode("+48", "PL"),
new CountryTelCode("+351", "PT"),
new CountryTelCode("+1-787", "PR"),
new CountryTelCode("+1-939", "PR"),
new CountryTelCode("+974", "QA"),
new CountryTelCode("+242", "CG"),
new CountryTelCode("+262", "RE"),
new CountryTelCode("+40", "RO"),
new CountryTelCode("+7", "RU"),
new CountryTelCode("+250", "RW"),
new CountryTelCode("+590", "BL"),
new CountryTelCode("+290", "SH"),
new CountryTelCode("+1-869", "KN"),
new CountryTelCode("+1-758", "LC"),
new CountryTelCode("+590", "MF"),
new CountryTelCode("+508", "PM"),
new CountryTelCode("+1-784", "VC"),
new CountryTelCode("+685", "WS"),
new CountryTelCode("+378", "SM"),
new CountryTelCode("+239", "ST"),
new CountryTelCode("+966", "SA"),
new CountryTelCode("+221", "SN"),
new CountryTelCode("+381", "RS"),
new CountryTelCode("+248", "SC"),
new CountryTelCode("+232", "SL"),
new CountryTelCode("+65", "SG"),
new CountryTelCode("+1-721", "SX"),
new CountryTelCode("+421", "SK"),
new CountryTelCode("+386", "SI"),
new CountryTelCode("+677", "SB"),
new CountryTelCode("+252", "SO"),
new CountryTelCode("+27", "ZA"),
new CountryTelCode("+82", "KR"),
new CountryTelCode("+211", "SS"),
new CountryTelCode("+34", "ES"),
new CountryTelCode("+94", "LK"),
new CountryTelCode("+249", "SD"),
new CountryTelCode("+597", "SR"),
new CountryTelCode("+47", "SJ"),
new CountryTelCode("+268", "SZ"),
new CountryTelCode("+46", "SE"),
new CountryTelCode("+41", "CH"),
new CountryTelCode("+963", "SY"),
new CountryTelCode("+886", "TW"),
new CountryTelCode("+992", "TJ"),
new CountryTelCode("+255", "TZ"),
new CountryTelCode("+66", "TH"),
new CountryTelCode("+228", "TG"),
new CountryTelCode("+690", "TK"),
new CountryTelCode("+676", "TO"),
new CountryTelCode("+1-868", "TT"),
new CountryTelCode("+216", "TN"),
new CountryTelCode("+90", "TR"),
new CountryTelCode("+993", "TM"),
new CountryTelCode("+1-649", "TC"),
new CountryTelCode("+688", "TV"),
new CountryTelCode("+1-340", "VI"),
new CountryTelCode("+256", "UG"),
new CountryTelCode("+380", "UA"),
new CountryTelCode("+971", "AE"),
new CountryTelCode("+44", "GB"),
new CountryTelCode("+1", "US"),
new CountryTelCode("+598", "UY"),
new CountryTelCode("+998", "UZ"),
new CountryTelCode("+678", "VU"),
new CountryTelCode("+379", "VA"),
new CountryTelCode("+58", "VE"),
new CountryTelCode("+84", "VN"),
new CountryTelCode("+681", "WF"),
new CountryTelCode("+212", "EH"),
new CountryTelCode("+967", "YE"),
new CountryTelCode("+260", "ZM"),
new CountryTelCode("+263", "ZW"),
};
}
If you're talking about phone calling codes you have to build a look-up table.
I would recommend looking this list of calling codes: Wikipedia
There's also quite a handy ReST API for this. I'd suggest copying the data locally into a json file or whatever rather than relying on a third-party service.
GET https://restcountries.eu/rest/v2/all
Related
How to draw a gradient outline facing outwards?
Using the system.drawing library in .net-core, is it possible given path: var bitmap = new Bitmap(1024, 512); var graphics = Graphics.FromImage(bitmap); var curve = new[] { new PointF(0f, 0f), new PointF(32f, 31.794779f), new PointF(64f, 62.367702f), new PointF(96f, 90.54387f), new PointF(128f, 115.24048f), new PointF(160f, 135.50848f), new PointF(192f, 150.56894f), new PointF(223.99998f, 159.84314f), new PointF(255.99998f, 162.97466f), new PointF(287.99997f, 159.84315f), new PointF(319.99997f, 150.56895f), new PointF(351.99997f, 135.50848f), new PointF(384f, 115.24048f), new PointF(416f, 90.54386f), new PointF(448f, 62.367676f), new PointF(480.00003f, 31.794739f), new PointF(512.00006f, -5.3103886E-05f), new PointF(544.00006f, -31.794844f), new PointF(576.00006f, -62.36777f), new PointF(608.00006f, -90.543945f), new PointF(640.00006f, -115.24056f), new PointF(672.00006f, -135.50854f), new PointF(704.0001f, -150.569f), new PointF(736.0001f, -159.84317f), new PointF(768.0001f, -162.97466f), new PointF(800.0001f, -159.84311f), new PointF(832.0001f, -150.56888f), new PointF(864.0002f, -135.50836f), new PointF(896.0002f, -115.24033f), new PointF(928.0002f, -90.543686f), new PointF(960.0002f, -62.36748f), new PointF(992.0002f, -31.794533f), new PointF(1024f, 2.8495393E-05f) }; var path = new GraphicsPath(); path.AddCurve(curve); graphics.TranslateTransform(0, 256); graphics.DrawPath(new Pen(Color.Black, 16), path); graphics.ResetTransform(); graphics.Dispose(); bitmap.Save("outline.png", ImageFormat.Png); To use an arbitary gradient: To draw a gradient outline that starts at the outer edges of the shape and progresses outwards to look like this? (Example was created via photopea.com layer style stroke, with a "Shape Burst" style gradient)
How to Use FontFamily in a concise manner
I'm new to C# programming and I have a function involving FontFamily. The code seems to work fine but I wanted it written in a more concise manner. I searched online but seems unable to get the right solution. The following code is what I currently have. public FontFamily[] FontFamilyExt() { FontFamily[] f = {new FontFamily(_fnt[0]), new FontFamily(_fnt[1]), new FontFamily(_fnt[2]), new FontFamily(_fnt[3]), new FontFamily(_fnt[4]), new FontFamily(_fnt[5]), new FontFamily(_fnt[6]), new FontFamily(_fnt[7]), new FontFamily(_fnt[8]), new FontFamily(_fnt[9]), new FontFamily(_fnt[10]), new FontFamily(_fnt[11]), new FontFamily(_fnt[12]), new FontFamily(_fnt[13]), new FontFamily(_fnt[14]), new FontFamily(_fnt[15]), new FontFamily(_fnt[16]), new FontFamily(_fnt[17]), new FontFamily(_fnt[18]), new FontFamily(_fnt[19]), new FontFamily(_fnt[20]), new FontFamily(_fnt[21]), new FontFamily(_fnt[22]), new FontFamily(_fnt[23]), new FontFamily(_fnt[24]), new FontFamily(_fnt[25]), new FontFamily(_fnt[26]), new FontFamily(_fnt[27]), new FontFamily(_fnt[28]), new FontFamily(_fnt[29]), new FontFamily(_fnt[30]), new FontFamily(_fnt[31]) }; return f; } I am looking for something that works similarly but uses less codes. Thank you in advance.
You could use a loop as recommended. You could also use Enumerable.Range, which basically creates a loop under the covers, but will put the code into one line. Something like: using System.Linq; public FontFamily[] FontFamilyExt() { return Enumerable.Range(0, 32).Select(x => new FontFamily(_fnt[x])).ToArray(); }
You can try this: public FontFamily[] FontFamilyExt() { FontFamily[] f = new FontFamily[32]; for ( int index = 0; index <= 31; index++ ) f[index] = new FontFamily(_fnt[index]); return f; }
How to add list, with LINQ, in an xml?
I have the following structure in an xml: <?xml version="1.0" encoding="utf-8"?> <ArrayOfNO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <No> <Id>1</Id> <Name>txt_1</Name> <Text>some text</Text> <Txt_relacionados> <string>txt_4</string> <string>txt_4</string> <string>txt_4</string> </Txt_relacionados> <N>0</N> <X>285</X> <Y>31</Y> <Externa>false</Externa> <EM>false</EM> <B>false</B> <K>false</K> <L1>false</L1> <L2>false</L2> <L3>false</L3> <L4>false</L4> <L5>false</L5> <L6>false</L6> <L7>false</L7> <L8>false</L8> <L9>false</L9> <L10>false</L10> <L11>false</L11> <L12>false</L12> <IP>0</IP> </No> I'm having trouble inserting the "Txt_relacionados" element because it contains a list of strings. In the VO of this element i have the following: [XmlArray("Txt_relacionados"), XmlArrayItem("string", Type = typeof(String))] private List<String> txt_relacionados = new List<String>(); public List<String> Txt_relacionados { get { return txt_relacionados; } set { txt_relacionados = value; } } What changes should I make in the code below to be able to insert this list? public void inserir2(VoNo value) { RefreshXDoc(); XElement no = XElement.Load(path); XElement element = new XElement("No", new XElement("Id", value.Id), new XElement("Name", value.Name), new XElement("Text", value.Text), new XmlArrayAttribute("Txt_relacionados", value.Txt_relacionados), new XElement("N", value.N), new XElement("X", value.X), new XElement("Y", value.Y), new XElement("Externa", value.Externa), new XElement("B", value.B), new XElement("K", value.K), new XElement("L1", value.L1), new XElement("L2", value.L2), new XElement("L3", value.L3), new XElement("L4", value.L4), new XElement("L5", value.L5), new XElement("L6", value.L6), new XElement("L7", value.L7), new XElement("L8", value.L8), new XElement("L9", value.L9), new XElement("L10", value.L10), new XElement("L11", value.L11), new XElement("L12", value.L12), new XElement("IP", value.IP)); no.Add(element); no.Save(path); } Thanks!!
You need to create Txt_relacionados element before you create new element No and then pass Txt_relacionados at proper place when you are creating No. Consider following. public void inserir2(VoNo value) { var rels = new XElement("Txt_relacionados"); foreach (var x in value.Txt_relacionados) { var elem = new XElement("string", x); rels.Add(elem); } RefreshXDoc(); XElement no = XElement.Load(path); XElement element = new XElement("No", new XElement("Id", value.Id), new XElement("Name", value.Name), new XElement("Text", value.Text), rels, new XElement("N", value.N), new XElement("X", value.X), new XElement("Y", value.Y), new XElement("Externa", value.Externa), new XElement("B", value.B), new XElement("K", value.K), new XElement("L1", value.L1), new XElement("L2", value.L2), new XElement("L3", value.L3), new XElement("L4", value.L4), new XElement("L5", value.L5), new XElement("L6", value.L6), new XElement("L7", value.L7), new XElement("L8", value.L8), new XElement("L9", value.L9), new XElement("L10", value.L10), new XElement("L11", value.L11), new XElement("L12", value.L12), new XElement("IP", value.IP)); no.Add(element); no.Save(path); }
Nesting XML Tags using Linq with Data From DB
I am trying to build an XML document using Linq, XElement and data from Database, It's working kinda, but in my XML, I want to close the tag and start a new tag and get the results from the query to populate into for the Tag, it is complaining that my variable r in the tag is unresolved, how can I make this work, or is there a better way of building the XML. All the child elements should be under the parent , having two children and , which has their own set of children. Here is the code below public void GenerateXML(int id, string site, string state, string country, string bFn, string bLn, string sFn, string sLn) { var results = (from o in _db.Orders where o.OrderId == id select o).ToList(); var xmlDoc = new XElement("Order", from r in results select new XElement("OrderHeader", new XElement("SiteId", site), new XElement("OrderId", r.OrderId), new XElement("Time", r.OrderDate.Value), new XElement("Subtotal", r.SubTotal), new XElement("Shipping", ""), new XElement("SalesTax", r.SalesTax), new XElement("Total", r.Total), new XElement("PaymentAmount", ""), new XElement("PaymentMethod", ""), new XElement("ArchiTypeAcctNum", "20001"), new XElement("TaxExempt", r.TaxExempt), new XElement("SpecialInstructions", r.SpecialInstructions), new XElement("BillTo", new XElement("BillEmail", r.BillToEmail), new XElement("FirstName", bFn), new XElement("LastName", bLn), new XElement("CompanyName", r.BillCompany), new XElement("Address1", r.BillToAddress), new XElement("City", r.BillToCity), new XElement("State", state), new XElement("Country", country), new XElement("Zip", r.BillToZip), new XElement("Phone", r.BillToPhoneNumber)), new XElement("ShipTo", new XElement("FirstName", sFn), new XElement("LastName", sLn), new XElement("CompanyName", r.ShipCompany), new XElement("Address1", r.ShipToAddress), new XElement("City", r.ShipToCity), new XElement("State", state), new XElement("Country", country), new XElement("Zip", r.ShipToZip), new XElement("Phone", r.ShipToPhoneNumber))), new XElement("Items", from i in r.Items select new XElement("Item", new XElement("SKU", i.SkuNumber), new XElement("PROD_Name", i.ProductName), new XElement("Description", i.Description), new XElement("Attributes", i.Attributes), new XElement("Quantity", i.Quantity), new XElement("UnitPrice", i.UnitPrice), new XElement("InkColor", i.InkColor))) ); xmlDoc.Save(Server.MapPath(#"~/Xml/Orders.xml")); RedirectToAction("Save"); }
I wrote an extension for same purpose. I think much easier . you can just use as orders.EntityToXml(); public static class XmlExtensions { public static bool EntityToXml<T>(this T entity, string filePath) { if (string.IsNullOrEmpty(filePath)) { throw new ArgumentNullException(nameof(filePath)); } var dir = Path.GetDirectoryName(filePath); if (string.IsNullOrEmpty(dir)) { throw new ArgumentNullException(nameof(filePath)); } if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var serializer= new System.Xml.Serialization.XmlSerializer(typeof(T)); using (var stream = new StreamWriter(filePath)) { serializer.Serialize(stream , entity); return true; } } }
Linq to XElement Preparation with some condtions
private XElement GetClinicalClientPrice(int invid) { using (MyDatabase db = new MyDatabase()) { return new XElement("CLINICALCLIENTPRICE", from ccp in db.TblClinicalClientPrice where ccp.INVENTORY_ID == invid select new XElement("Record", new XElement("ITEM_ID",ccp.ITEM_ID), new XElement("INVENTORY_ID", ccp.INVENTORY_ID), new XElement("ITEM_DESCRIPTION", ccp.ITEM_DESCRIPTION), new XElement("SUPPLIER_CAT", ccp.SUPPLIER_CAT), new XElement("NDC_CODE", ccp.NDC_CODE), new XElement("ITEM_NUMBER", ccp.ITEM_NUMBER), new XElement("UOM", ccp.UOM), new XElement("CONVERSION_FACTOR", ccp.CONVERSION_FACTOR), new XElement("MANUFACTURER", ccp.MANUFACTURER), new XElement("SESSION_ID", ccp.SESSION_ID), new XElement("USERNAME", ccp.USERNAME), new XElement("ENTRY_TIME", ccp.ENTRY_TIME), new XElement("COST", ccp.COST), new XElement("CLIENT_INPUT_ID", ccp.CLIENT_INPUT_ID), new XElement("MHS_ITEM_ID", ccp.MHS_ITEM_ID), new XElement("GUID", ccp.GUID), new XElement("UNIQUE_ID", ccp.UNIQUE_ID), new XElement("SUPPLIER_CAT_MODIFIED", ccp.SUPPLIER_CAT_MODIFIED), new XElement("SIZE", ccp.SIZE), new XElement("STRENGTH", ccp.STRENGTH) )); } } I am getting (DateTime) Null value exception when I am inserting above resulted XElement into data base. My Example is : stackoverflow.com/a/21163312/3121707