Input for WinForms | ComponentOne
In This Topic
    Country.cs
    In This Topic
    Country
    Copy Code
    namespace CountryCity
    {
        [Serializable]
        public class Country
        {
            public Country() { }
            public Country(string name, City[] cities)
            {
                Name = name;
                Cities.AddRange(cities);
            }
            private List<City> _cities;
            private City[] _cityArray;
            public string Name { get; set; }
            public string ISO2Name { get; set; }
            public string ISO3Name { get; set; }
    
            public City[] CityArray
            {
                get { return Cities.ToArray(); }
                set
                {
                    _cityArray = value;
                    if (_cityArray != null)
                    {
                        Cities.Clear();
                        foreach (City city in _cityArray)
                        {
                            city.Country = this;
                            Cities.Add(city);
                        }
                    }
                }
            }
            [XmlIgnore]
            public List<City> Cities
            {
                get
                {
                    if (_cities is null)
                    {
                        _cities = new List<City>();
                    }
                    return _cities;
                }
                set
                {
                    _cities = value;
                    foreach (City city in _cities)
                    {
                        city.Country = this;
                    }
                }
            }
    
            public static List<Country> GetCountries()
            {
                XmlSerializer xs = new XmlSerializer(typeof(List<Country>));
                XmlReader xr = XmlReader.Create("CountryCityConfig.xml");
                return (List<Country>)xs.Deserialize(xr);
    
            }
        }
    
        public class City
        {
            public City() { }
            public City(string name)
            {
                CityName = name;
            }
            public string CityName { get; set; }
    
            [XmlIgnore]
            public Country Country { get; set; }
        }
    }