Ejemplo n.º 1
0
        /// <summary>
        /// Code Executed When Button Search is Clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchButton_Click(object sender, EventArgs e)
        {
            textBoxResult.Text = ""; // Set Initially textBoxResult as Empty
            labelCount.Text    = ""; // Set Initially labelCount as Empty

            // Get City details from the textbox named textboxCity
            string cityName = textboxCity.Text;

            // Regular Expression for special character
            Regex specialCharacterRegex = new Regex(@"[~`!@#$%^&*()+=|\\{}':;.,<>/?[\]""_-]");
            int   _result; // variable to hold int value returned by TryParse

            // Display Error in case of not provided city name or numbers provided
            if (cityName.Length == 0 || int.TryParse(cityName, out _result))
            {
                labelError.Visible         = true;
                labelError.Text            = Errors.InvalidCityInputError;
                pictureBoxLocation.Visible = true;
                labelResult.Visible        = false;
                textBoxResult.Visible      = false;
            }

            // To check occurence of any special character by matching it with Regular
            // Expression defined by specialCharacterRegex
            else if (specialCharacterRegex.IsMatch(cityName))
            {
                labelError.Visible         = true;
                labelError.Text            = Errors.SpecialCharacterError;
                pictureBoxLocation.Visible = true;
                labelResult.Visible        = false;
                textBoxResult.Visible      = false;
            }
            // Fetch the Companies Details and display it
            else
            {
                int      _countCompanyName = 0;
                string   formattedCityName = string.Empty; // string to format the provided city name in reuired url
                string[] cityInfo          = cityName.Split(' ');

                // format the provided city name to their respective url
                foreach (string word in cityInfo)
                {
                    formattedCityName += word + "+";
                }


                _baseUrl = GetBaseUrl(formattedCityName);

                // calling method with city name as passed parameter
                GetCompaniesInXml(_baseUrl);

                // Getting all the IT Companies name with tag 'name' in XML file to XmlNodeList object
                XmlNodeList nodeListName = _xmlDoc.GetElementsByTagName("name");

                // Getting all the IT Companies address with tag 'formatted_address' in XML file to XmlNodeList object
                XmlNodeList nodeListAddress = _xmlDoc.GetElementsByTagName("formatted_address");

                // Count IT Companies for provided Location
                _countCompanyName = nodeListName.Count;

                // Getting token name for returned result
                _nodeListToken = _xmlDoc.GetElementsByTagName("next_page_token");

                // Append _pageTokenList with latest token
                foreach (XmlNode xn in _nodeListToken)
                {
                    _pageTokenList.Add(_nodeListToken[0].InnerText);
                }

                // nextButton method will not be called in case of no token available
                if (_pageTokenList.Count > 0)
                {
                    buttonNext.Visible = true;
                }


                // Create object for all the available IT Company
                CompanyDetails[] company = new CompanyDetails[_countCompanyName];

                // Set name and address for each company in CompanyDetails object
                for (int i = 0; i < _countCompanyName; i++)
                {
                    company[i] = new CompanyDetails();
                    company[i].set_Name(nodeListName[i].InnerText);
                    company[i].set_Address(nodeListAddress[i].InnerText);
                }

                // Dispaly Error in case of no returned Company Name
                if (_countCompanyName == 0)
                {
                    labelError.Visible         = true;
                    labelError.Text            = Errors.InvalidCityNameError;
                    pictureBoxLocation.Visible = true;
                    labelResult.Visible        = false;
                    textBoxResult.Visible      = false;
                }

                // Append Each Object to textBoxResult
                else
                {
                    for (int i = 0; i < _countCompanyName; i++)
                    {
                        labelError.Visible         = false;
                        pictureBoxLocation.Visible = false;
                        labelResult.Visible        = true;
                        textBoxResult.Visible      = true;

                        // Appending textBoxResult with CompanyDetails Object by calling toString() method for each object
                        textBoxResult.Text += company[i] + "\r\n-------------------------------------------------------------\r\n";
                    }

                    // Display the number of IT Companies on label named labelCount
                    labelCount.Visible = true;
                    labelCount.Text    = _countCompanyName.ToString();
                }
            }
        }
Ejemplo n.º 2
0
        private void buttonNext_Click(object sender, EventArgs e)
        {
            textBoxResult.Text = ""; // Set Initially textBoxResult as Empty
            labelCount.Text    = ""; // Set Initially labelCount as Empty

            // Get City details from the textbox named textboxCity
            string cityName = textboxCity.Text;


            int    _countCompanyName = 0;
            string formattedCityName = string.Empty; // string to format the provided city name in reuired url

            string[] cityInfo = cityName.Split(' ');

            // format the provided city name to their respective url
            foreach (string word in cityInfo)
            {
                formattedCityName += word + "+";
            }

            int countPageToken = _pageTokenList.Count;

            _baseUrl = GetBaseUrl(formattedCityName, _pageTokenList[countPageToken - 1]);

            // calling method with city name as passed parameter
            GetCompaniesInXml(_baseUrl);

            // Getting all the IT Companies name with tag 'name' in XML file to XmlNodeList object
            XmlNodeList nodeListName = _xmlDoc.GetElementsByTagName("name");

            // Getting all the IT Companies address with tag 'formatted_address' in XML file to XmlNodeList object
            XmlNodeList nodeListAddress = _xmlDoc.GetElementsByTagName("formatted_address");

            // Getting token name for returned result
            _nodeListToken = _xmlDoc.GetElementsByTagName("next_page_token");

            // In case of no token available, it will disable the next button
            if (_nodeListToken.Count == 0)
            {
                buttonNext.Visible = true;
            }

            // Append _pageTokenList with latest token
            foreach (XmlNode xn in _nodeListToken)
            {
                _pageTokenList.Add(_nodeListToken[0].InnerText);
            }



            // Count IT Companies for provided Location
            _countCompanyName = nodeListName.Count;

            // Create object for all the available IT Company
            CompanyDetails[] company = new CompanyDetails[_countCompanyName];

            // Set name and address for each company in CompanyDetails object
            for (int i = 0; i < _countCompanyName; i++)
            {
                company[i] = new CompanyDetails();
                company[i].set_Name(nodeListName[i].InnerText);
                company[i].set_Address(nodeListAddress[i].InnerText);
            }

            // Dispaly Error in case of no returned Company Name
            if (_countCompanyName == 0)
            {
                labelError.Visible         = true;
                labelError.Text            = Errors.InvalidCityNameError;
                pictureBoxLocation.Visible = true;
                labelResult.Visible        = false;
                textBoxResult.Visible      = false;
            }

            // Append Each Object to textBoxResult
            else
            {
                for (int i = 0; i < _countCompanyName; i++)
                {
                    labelError.Visible         = false;
                    pictureBoxLocation.Visible = false;
                    labelResult.Visible        = true;
                    textBoxResult.Visible      = true;
                    // Appending textBoxResult with CompanyDetails Object by calling toString() method for each object
                    textBoxResult.Text += company[i] + "\r\n-------------------------------------------------------------\r\n";
                }

                // Display the number of IT Companies on label named labelCount
                labelCount.Visible = true;
                labelCount.Text    = _countCompanyName.ToString();
            }
        }