/// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ValidationEventHandler(object sender, ValidationEventArgs e)
        {
            try
            {
                var dataType = "";
                if (e.Severity == XmlSeverityType.Warning)
                {
                    Console.Write("WARNING: ");
                    Console.WriteLine(e.Message);
                }
                else if (e.Severity == XmlSeverityType.Error)
                {
                    if (!errorDetected)
                    {
                        this.errorDetected = true;
                        #region ApplicationLogEntries DB Entry
                        ApplicationEventMaster Event = unitOfWork.ApplicationEventMasterRepository
                                                       .SelectByID(Constants.EventType.Standard_Xml_Validation_Failed);

                        ApplicationMaster Application = unitOfWork.ApplicationMasterRepository
                                                        .SelectByID(Constants.ApplicationId);

                        ApplicationLog objAppLog = unitOfWork.CreateApplicationLogObject(Event, Application,
                                                                                         "LoremIpsumMessage", "amjad.leghari");

                        unitOfWork.ApplicationLogRepository.Insert(objAppLog);
                        unitOfWork.Save();
                        #endregion
                    }

                    IXmlLineInfo node = sender as IXmlLineInfo;
                    if (node != null && node.HasLineInfo())
                    {
                        Console.WriteLine(node.LinePosition);
                        Console.WriteLine(node.LineNumber);
                    }

                    Console.Write("ERROR: ");
                    Console.WriteLine(e.Message);

                    Console.Write("DataKey: ");
                    var obj = sender as XmlReader;
                    Console.WriteLine(obj.Name);
                    Console.Write("DataValue: ");
                    Console.WriteLine(obj.Value);

                    if (obj.SchemaInfo.SchemaElement != null)
                    {
                        Console.Write("DataType: ");
                        dataType = obj.SchemaInfo.SchemaElement.ElementSchemaType.Datatype.TypeCode.ToString();
                        Console.WriteLine(dataType);
                    }
                    if (String.IsNullOrEmpty(dataType))
                    {
                        if (e.Message.Contains("cannot appear more than once"))
                        {
                            dataType = "duplicate elements";
                        }
                    }
                    var xPath = elementStack.Reverse().Aggregate(string.Empty, (x, y) => x + "/" + y);
                    if (!xPath.Contains(obj.Name))
                    {
                        xPath += String.Format("/{0}", obj.Name);
                    }
                    Console.Write("XPath: ");
                    Console.WriteLine(xPath);

                    #region ErrorXml DB Entry
                    if (currentErrorXml == null)
                    {
                        currentErrorXml = unitOfWork.CreateErrorXml(
                            clientCode,
                            warehouseCode,
                            String.IsNullOrEmpty(orderNumber)
                                    ? "dummy order number"
                                    : orderNumber,
                            this.varDocumentType,
                            orderDate,
                            DateTime.Now, this.inProcessXML);

                        unitOfWork.ErrorXmlRepository.Insert(currentErrorXml);
                        unitOfWork.Save();
                    }
                    #endregion
                    //Attribute temp = this.LocalAttributes.Where(attr => attr.name.Equals(obj.Name)).First<Attribute>();

                    bool IsRectifiable = /*(temp!=null) ? temp.is_rectifiable : */ false;
                    if (obj.SchemaInfo != null)
                    {
                        if (obj.SchemaInfo.SchemaElement != null)
                        {
                            if (obj.SchemaInfo.SchemaElement.UnhandledAttributes != null)
                            {
                                //if (obj.SchemaInfo.SchemaElement.moreAttributes.Count > 0)
                                if (obj.SchemaInfo.SchemaElement.UnhandledAttributes.Count() > 0)
                                {
                                    if (obj.SchemaInfo.SchemaElement.UnhandledAttributes
                                        .Where(attr => attr.Name.Equals("")).Count() > 0)
                                    {
                                        IsRectifiable = Convert.ToBoolean(obj.SchemaInfo
                                                                          .SchemaElement.UnhandledAttributes
                                                                          .Where(attr => attr.Name.Equals("")).First <XmlAttribute>().Value);
                                    }
                                }
                            }
                        }
                    }

                    #region ErrorInboundData DB Entry
                    ErrorInboundData objErrInbound = unitOfWork.CreateErrorInboundData(obj.Name,
                                                                                       xPath, dataType, obj.Value, this.validator_type, e.Message, "not available",
                                                                                       unitOfWork.ErrorXmlRepository.SelectByID(currentErrorXml.Id), IsRectifiable);

                    unitOfWork.ErrorInboundRepository.Insert(objErrInbound);
                    unitOfWork.Save();
                    #endregion

                    #region ErrorSuggestion DB Entry

                    Error_Suggestion_InboundData_Mapper esidmapObj = new Error_Suggestion_InboundData_Mapper();
                    esidmapObj.Id = Guid.NewGuid();
                    switch (objErrInbound.DataType.ToLower())
                    {
                    case "integer":
                        esidmapObj.ErrorSuggestionId = Constants.Suggestions.XSD_Invalid_Integer;
                        break;

                    case "float":
                        esidmapObj.ErrorSuggestionId = Constants.Suggestions.XSD_Invalid_Float;
                        break;

                    case "datetime":
                        esidmapObj.ErrorSuggestionId = Constants.Suggestions.XSD_Invalid_DateTime;
                        break;

                    case "boolean":
                        esidmapObj.ErrorSuggestionId = Constants.Suggestions.XSD_Invalid_Boolean;
                        break;

                    case "duplicate elements":
                        esidmapObj.ErrorSuggestionId = Constants.Suggestions.XSD_Duplicate_Element;
                        break;

                    case "string":
                        esidmapObj.ErrorSuggestionId = Constants.Suggestions.XSD_Invalid_String;
                        break;

                    default:        break;
                    }

                    esidmapObj.ErrorInboundDataId = objErrInbound.Id;
                    esidmapObj.DateTime           = DateTime.Now;
                    unitOfWork.ErrorSuggestion_InboundDataRepository.Insert(esidmapObj);
                    unitOfWork.Save();
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Logger.Instance.GetLogInstance().Error(JsonConvert.SerializeObject(ex));
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool ValidateAddressFields()
        {
            bool retVal = false;
            List <Location_SYD> locations = null;
            int validationPassCount       = 0;

            while (true)
            {
                if (validationPassCount > 1)
                {
                    break;
                }
                // If postCode is international(9999)
                // Then set state as 'ITL'
                // Return 1;
                if (this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.postCode).First <ValidationRuleEngine.Configuration.Attribute>().value.Equals("9999"))
                {
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.state).First <ValidationRuleEngine.Configuration.Attribute>().value = "ITL";
                    retVal = true;
                    break;
                }

                // If String.IsNullorEmpty(varLocation) == true
                // Then return -1;
                // Else If select row in Location_Syd
                // Where (Location_Syd.postCode == varPostCode)
                // && (Location_Syd.location == varLocation)
                // && (Location_Syd.state == varState)
                // Then return 0;
                if (string.IsNullOrEmpty(this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.location).First <ValidationRuleEngine.Configuration.Attribute>().value))
                {
                    // Make entry in ErrorXML, ErrorInboundData & ErrorSuggestion
                    #region ErrorXml
                    ErrorXml objErrorXml = unitOfWork.CreateErrorXml(
                        clientCode,
                        warehouseCode,
                        orderNumber,
                        varDocumentType,
                        orderDate,
                        DateTime.Now, string.IsNullOrEmpty(this.inProcessXML)
                                            ? "empty xml"
                                            : this.inProcessXML);

                    unitOfWork.ErrorXmlRepository.Insert(objErrorXml);
                    unitOfWork.Save();
                    #endregion

                    #region ErrorInboundData
                    bool IsRectifiable = false;
                    IsRectifiable = this.LocalAttributes.Where(attr => attr.name.Equals(Constants.AddressValidator_CustomFields.location)).First().is_rectifiable;
                    ErrorInboundData errInboundData = unitOfWork.CreateErrorInboundData("Suburb",
                                                                                        this.LocalAttributes
                                                                                        .Where(item => item.name == Constants.AddressValidator_CustomFields.location)
                                                                                        .First <ValidationRuleEngine.Configuration.Attribute>().path,
                                                                                        "String",
                                                                                        this.LocalAttributes
                                                                                        .Where(item => item.name == Constants.AddressValidator_CustomFields.location)
                                                                                        .First <ValidationRuleEngine.Configuration.Attribute>().value, this.validator_type,
                                                                                        "Invalid suburb value", "Suburb value is either null/empty or invalid",
                                                                                        objErrorXml, IsRectifiable);

                    unitOfWork.ErrorInboundRepository.Insert(errInboundData);
                    unitOfWork.Save();
                    #endregion

                    #region ErrorSuggestion
                    Error_Suggestion_InboundData_Mapper esidmapObj = new Error_Suggestion_InboundData_Mapper();
                    esidmapObj.Id = Guid.NewGuid();
                    esidmapObj.ErrorSuggestionId  = Constants.Suggestions.InvalidSuburb;
                    esidmapObj.ErrorInboundDataId = errInboundData.Id;
                    esidmapObj.DateTime           = DateTime.Now;
                    unitOfWork.ErrorSuggestion_InboundDataRepository.Insert(esidmapObj);
                    unitOfWork.Save();
                    #endregion
                    retVal = false;
                    break;
                }
                else
                {
                    locations = unitOfWork.LocationRepository.SelectByLocationPostCodeAndState(
                        this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.location).First <ValidationRuleEngine.Configuration.Attribute>().value,
                        this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.postCode).First <ValidationRuleEngine.Configuration.Attribute>().value,
                        this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.state).First <ValidationRuleEngine.Configuration.Attribute>().value,
                        LocationMatch.WithoutModification);

                    if (locations.Count == 1)
                    {
                        retVal = true;
                        break;
                    }
                }

                // ElseIf Select row in Location_Syd
                // Where (Location_Syd.location == varLocation after removing () part )
                // && (Location_Syd.state == varState)
                // && (Location_Syd.postCode == varPostCode)
                // Then return 1;
                // e.g if arg.Location == 'ASCOT' && location_Syd.location in
                // {
                //      'ASCOT (BENDIGO)',
                //      'ASCOT (CRESWICK)',
                //      'ASCOT (CAMBOOYA)'
                // }
                //LocationCustomRepository locationCustomRepository = new LocationCustomRepository();
                locations = unitOfWork.LocationRepository.SelectByLocationPostCodeAndState(
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.location).First <ValidationRuleEngine.Configuration.Attribute>().value,
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.postCode).First <ValidationRuleEngine.Configuration.Attribute>().value,
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.state).First <ValidationRuleEngine.Configuration.Attribute>().value,
                    LocationMatch.StripParenthesis);
                if (locations.Count == 1)
                {
                    retVal = true;
                    break;
                }

                // If ((Location_Syd.location == varLocation) && (Location_Syd.postcode == varPostCode))
                // set varState = Location_Syd.state;
                locations = unitOfWork.LocationRepository.SelectByLocationAndPostCode(
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.location).First <ValidationRuleEngine.Configuration.Attribute>().value,
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.postCode).First <ValidationRuleEngine.Configuration.Attribute>().value,
                    LocationMatch.WithoutModification);
                if (locations.Count > 0)
                {
                    if (locations.Count == 1)
                    {
                        this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.state).First <ValidationRuleEngine.Configuration.Attribute>().value = locations[0].state;
                        retVal = true;
                        break;
                    }
                }

                // If ((Location_Syd.location == varLocation.strip parenthesis part) && (Location_Syd.state == varState)
                // && (String.IsNullorEmptyString(varPostCode)||varPostCode does not exist in DB)
                // set varPostCode = Location_Syd.postcode;
                locations = unitOfWork.LocationRepository.SelectByLocationAndPostCode(
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.location).First <ValidationRuleEngine.Configuration.Attribute>().value,
                    this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.postCode).First <ValidationRuleEngine.Configuration.Attribute>().value,
                    LocationMatch.StripParenthesis);
                if (locations.Count > 0)
                {
                    if (locations.Count == 1)
                    {
                        this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.state).First <ValidationRuleEngine.Configuration.Attribute>().value = locations[0].state;
                        retVal = true;
                        break;
                    }
                }

                // If ( (String.IsNullorEmptyString(varPostCode)||(varPostCode does not exist in DB))
                // && String.IsNullorEmptyString(varState))
                // && varLocation == Location_Syd.location.strip parenthesis part)
                // set varPostCode = Location_Syd.postcode;
                // set varState = Location_Syd.state;
                // if(rowset>1) return -1
                // else
                // set varPostCode = Location_Syd.postcode;
                // set varState = Location_Syd.state;

                if (
                    !string.IsNullOrEmpty(this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.state).First <ValidationRuleEngine.Configuration.Attribute>().value) &&
                    (
                        (!unitOfWork.LocationRepository.DoesPostCodeExist(this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.postCode).First <ValidationRuleEngine.Configuration.Attribute>().value)) ||
                        string.IsNullOrEmpty(this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.postCode).First <ValidationRuleEngine.Configuration.Attribute>().value)
                    )
                    )
                {
                    var Locations = unitOfWork.LocationRepository.GetLocationByLocation(this.LocalAttributes.Where(item => item.name == Constants.AddressValidator_CustomFields.location).First <ValidationRuleEngine.Configuration.Attribute>().value, LocationMatch.StripParenthesis);
                    if (Locations.Count > 0)
                    {
                        if (Locations.Count > 1)
                        {
                            #region ErrorXml
                            ErrorXml objErrorXml = unitOfWork.CreateErrorXml(
                                clientCode,
                                warehouseCode,
                                orderNumber,
                                varDocumentType,
                                orderDate,
                                DateTime.Now,
                                string.IsNullOrEmpty(this.inProcessXML) ? "test xml" : this.inProcessXML);

                            unitOfWork.ErrorXmlRepository.Insert(objErrorXml);
                            unitOfWork.Save();
                            #endregion

                            #region ErrorInboundData
                            bool IsRectifiable = false;
                            IsRectifiable = this.LocalAttributes.Where(attr => attr.name.Equals(Constants.AddressValidator_CustomFields.location)).First().is_rectifiable;

                            ErrorInboundData errInboundData = unitOfWork.CreateErrorInboundData("Suburb",
                                                                                                this.LocalAttributes
                                                                                                .Where(item => item.name == Constants.AddressValidator_CustomFields.location)
                                                                                                .First <ValidationRuleEngine.Configuration.Attribute>().path,
                                                                                                "String",
                                                                                                this.LocalAttributes
                                                                                                .Where(item => item.name == Constants.AddressValidator_CustomFields.location)
                                                                                                .First <ValidationRuleEngine.Configuration.Attribute>().value,
                                                                                                this.validator_type, "Suburb value is either null/empty or invalid",
                                                                                                "", objErrorXml, IsRectifiable);
                            unitOfWork.ErrorInboundRepository.Insert(errInboundData);
                            unitOfWork.Save();
                            #endregion

                            #region ErrorSuggestion
                            Error_Suggestion_InboundData_Mapper esidmapObj = new Error_Suggestion_InboundData_Mapper();
                            esidmapObj.Id = Guid.NewGuid();
                            esidmapObj.ErrorSuggestionId  = Constants.Suggestions.InvalidSuburbPostCodeState;
                            esidmapObj.ErrorInboundDataId = errInboundData.Id;
                            esidmapObj.DateTime           = DateTime.Now;
                            unitOfWork.ErrorSuggestion_InboundDataRepository.Insert(esidmapObj);
                            unitOfWork.Save();
                            #endregion

                            retVal = false;
                            break;
                        }
                        else
                        {
                            this.LocalAttributes
                            .Where(item => item.name == Constants.AddressValidator_CustomFields.state)
                            .First <ValidationRuleEngine.Configuration.Attribute>().value = Locations[0].state;
                            this.LocalAttributes
                            .Where(item => item.name == Constants.AddressValidator_CustomFields.postCode)
                            .First <ValidationRuleEngine.Configuration.Attribute>().value = Locations[0].postcode;
                        }
                    }
                }

                // If ( (varPostCode == Location_Syd.postcode)
                // && (Location_Syd.location.substring(0,5) == varlocation.substring(0,5)))
                // set varPostCode = Location_Syd.postcode;
                // set varState = Location_Syd.state;
                // set varLocation = Location_Syd.location;
                locations = unitOfWork.LocationRepository.SelectByLocationAndPostCode(
                    this.LocalAttributes
                    .Where(item => item.name == Constants.AddressValidator_CustomFields.location)
                    .First <ValidationRuleEngine.Configuration.Attribute>().value,
                    this.LocalAttributes
                    .Where(item => item.name == Constants.AddressValidator_CustomFields.postCode)
                    .First <ValidationRuleEngine.Configuration.Attribute>().value,
                    LocationMatch.MatchFirst5Chars);
                if (locations.Count > 0)
                {
                    if (locations.Count == 1)
                    {
                        this.LocalAttributes
                        .Where(item => item.name == Constants.AddressValidator_CustomFields.location)
                        .First <ValidationRuleEngine.Configuration.Attribute>().value = locations[0].location;
                        this.LocalAttributes
                        .Where(item => item.name == Constants.AddressValidator_CustomFields.state)
                        .First <ValidationRuleEngine.Configuration.Attribute>().value = locations[0].state;
                        this.LocalAttributes
                        .Where(item => item.name == Constants.AddressValidator_CustomFields.postCode)
                        .First <ValidationRuleEngine.Configuration.Attribute>().value = locations[0].postcode;
                    }
                }

                // if varLocation has parenthesis then possibility is that there is state in parenthesis.
                // if (varLocation.parenthesis part == state)
                // set varState=Location_Syd.state;
                // rerun all the checks
                // if varLocation spaces state-3chars
                // set varState=Location_Syd.state
                // rerun all the checks

                validationPassCount++;
                string stateTemp    = ExtractStateFromLocation();
                string LocationTemp = ExtractLocationStrippingParenthesisOrSpaces();
                if (!String.IsNullOrEmpty(stateTemp))
                {
                    var tempLocations = unitOfWork.LocationRepository.SelectByLocationPostCodeAndState(LocationTemp,
                                                                                                       this.LocalAttributes
                                                                                                       .Where(item => item.name == Constants.AddressValidator_CustomFields.postCode)
                                                                                                       .First <ValidationRuleEngine.Configuration.Attribute>().value,
                                                                                                       stateTemp, LocationMatch.StripParenthesis);
                    if (tempLocations.Count > 1 || tempLocations.Count == 0)
                    {
                        #region ErrorXml
                        ErrorXml objErrorXml = unitOfWork.CreateErrorXml(
                            clientCode,
                            warehouseCode,
                            orderNumber,
                            varDocumentType,
                            orderDate,
                            DateTime.Now, String.IsNullOrEmpty(this.inProcessXML)
                                                ? "test xml"
                                                : this.inProcessXML);

                        unitOfWork.ErrorXmlRepository.Insert(objErrorXml);
                        unitOfWork.Save();
                        #endregion

                        #region ErrorInboundData
                        bool IsRectifiable = false;
                        IsRectifiable = this.LocalAttributes.Where(attr => attr.name.Equals(Constants.AddressValidator_CustomFields.location)).First().is_rectifiable;

                        ErrorInboundData errInboundData = unitOfWork.CreateErrorInboundData("(Suburb / Postcode / State)",
                                                                                            "",
                                                                                            "String",
                                                                                            "", this.validator_type,
                                                                                            "One of the field(Suburb / Postcode / State) value is either null/empty or invalid",
                                                                                            "", objErrorXml, IsRectifiable);

                        unitOfWork.ErrorInboundRepository.Insert(errInboundData);
                        unitOfWork.Save();
                        #endregion

                        #region ErrorSuggestion
                        Error_Suggestion_InboundData_Mapper esidmapObj = new Error_Suggestion_InboundData_Mapper();
                        esidmapObj.Id = Guid.NewGuid();
                        esidmapObj.ErrorSuggestionId  = Constants.Suggestions.InvalidSuburbPostCodeState;
                        esidmapObj.ErrorInboundDataId = errInboundData.Id;
                        esidmapObj.DateTime           = DateTime.Now;
                        unitOfWork.ErrorSuggestion_InboundDataRepository.Insert(esidmapObj);
                        unitOfWork.Save();
                        #endregion

                        retVal = false;
                        break;
                    }
                    else
                    {
                        this.LocalAttributes
                        .Where(item => item.name == Constants.AddressValidator_CustomFields.location)
                        .First <ValidationRuleEngine.Configuration.Attribute>().value = tempLocations[0].location;
                        this.LocalAttributes
                        .Where(item => item.name == Constants.AddressValidator_CustomFields.state)
                        .First <ValidationRuleEngine.Configuration.Attribute>().value = tempLocations[0].state;
                        continue;
                    }
                }
            }
            return(retVal);
        }