예제 #1
0
        /// <summary>
        /// Gets the universities based on a US State
        /// </summary>
        /// <param name="state">Either the name or the postal code.</param>
        /// <returns></returns>
        public static AmericanUniversity[] GetUniversitiesByState(string state)
        {
            UnivXml = UnivXml ?? Core.XmlDocXrefIdentifier.GetEmbeddedXmlDoc(US_UNIVERSITY_DATA,
                                                                             Assembly.GetExecutingAssembly());
            //this will never pass so avoid the exception
            if (UnivXml == null)
            {
                return new AmericanUniversity[] { }
            }
            ;

            if (String.IsNullOrWhiteSpace(state))
            {
                _allStateAbbreviations = _allStateAbbreviations ?? GetAllXmlStateAbbreviations(UnivXml);

                state = GetRandomStateAbbrev(_allStateAbbreviations);
            }

            var qryBy = "name";

            if (state.Length == 2)
            {
                qryBy = "abbreviation";
            }
            else
            {
                state = string.Join(" ", NfString.DistillToWholeWords(state));
            }

            var elements =
                UnivXml.SelectSingleNode($"//state[@{qryBy}='{state}']");

            if (elements == null || !elements.HasChildNodes)
            {
                return new AmericanUniversity[] { }
            }
            ;

            var tempList = new List <AmericanUniversity>();

            foreach (var elem in elements)
            {
                if (TryParseXml(elem as XmlElement, out var univOut))
                {
                    univOut.StateName = state;
                    tempList.Add(univOut);
                }
            }

            if (tempList.Count == 0)
            {
                return new AmericanUniversity[] { }
            }
            ;

            return(tempList.ToArray());
        }