Ejemplo n.º 1
0
        /// <summary>
        /// Gets High Schools based on US Postal Zip Code
        /// </summary>
        /// <param name="zipCode">
        /// Either a 3 digit prefix or the full 5 digit code.
        /// </param>
        /// <returns></returns>
        public static AmericanHighSchool[] GetHighSchoolsByZipCode(string zipCode)
        {
            zipCode = zipCode ?? "";
            zipCode = new string(zipCode.ToCharArray().Where(char.IsDigit).ToArray());
            if (string.IsNullOrWhiteSpace(zipCode))
            {
                return(GetHighSchoolsByState());
            }

            HsXml = HsXml ?? XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_HIGH_SCHOOL_DATA,
                                                                    Assembly.GetExecutingAssembly());
            if (HsXml == null)
            {
                return new[] { GetDefaultHs() }
            }
            ;

            var xpath = zipCode.Length == 3
                        ? $"//zip-code[@prefix='{zipCode}']//high-school"
                        : $"//zip-stat[@value='{zipCode}']/high-school";

            //just get this once for all
            var stateXpath  = $"{xpath}/../../..";
            var stateName   = "";
            var stateAbbrev = "";
            var stateNode   = HsXml.SelectSingleNode(stateXpath);

            if (stateNode?.Attributes != null)
            {
                var attr = stateNode.Attributes["name"];

                stateName = attr.Value;

                attr        = stateNode.Attributes["abbreviation"];
                stateAbbrev = attr.Value;
            }

            var elements = HsXml.SelectNodes(xpath);

            if (elements == null || elements.Count <= 0)
            {
                return(GetHighSchoolsByState());
            }

            var parsedList = ParseHighSchoolsFromNodeList(elements);

            foreach (var hs in parsedList)
            {
                hs.StateAbbrev = stateAbbrev;
                hs.StateName   = stateName;
            }
            return(parsedList);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets High Schools based on US State
        /// </summary>
        /// <param name="state">
        /// Gets either a specific states high schools or picks a state at random if missing.
        /// Use either the name or the postal code.
        /// </param>
        /// <returns></returns>
        public static AmericanHighSchool[] GetHighSchoolsByState(string state = null)
        {
            HsXml = HsXml ?? XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_HIGH_SCHOOL_DATA,
                                                                    Assembly.GetExecutingAssembly());
            if (HsXml == null)
            {
                return new[] { GetDefaultHs() }
            }
            ;
            if (String.IsNullOrWhiteSpace(state))
            {
                _allStateAbbreviations = _allStateAbbreviations ?? GetAllXmlStateAbbreviations(HsXml);

                state = GetRandomStateAbbrev(_allStateAbbreviations);
            }

            //have this deal with if its a postal code or a full name
            var usState = UsState.GetState(state);

            if (usState == null)
            {
                return new[] { GetDefaultHs() }
            }
            ;

            var elements =
                HsXml.SelectNodes($"//state[@name='{usState}']//high-school");

            var parsedList = ParseHighSchoolsFromNodeList(elements);

            foreach (var hs in parsedList)
            {
                hs.StateAbbrev = usState.StateAbbrev;

                hs.StateName = usState.ToString();
            }

            return(parsedList);
        }