/// <summary>
        /// Controls creation of a GovGateway GiftAid submission message based on input data
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="ofile"></param>
        /// <param name="env"></param>
        private static void _createMessage(DataTable dt, string ofile, string env)
        {
            try
            {
                log.Debug("GovTalkGateway message creation started");
                GovTalkGateway Submission = new GovTalkGateway(dt);
                GovTalkMessage NewGovTalkMessage = Submission.CreateRequestMessage(env);
                GiftAidSubmissionProcessController.SerializeObjectToFile(NewGovTalkMessage, ofile);

                log.Debug("File created successfully");
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                throw ex;
            }
        }
        /// <summary>
        /// Resumes an unfinished request based on the correlation ID
        /// </summary>
        /// <param name="correlationId"></param>
        public static void ResumePendingRequest(string correlationId, string PollMsgFileName, string PollMsgResults)
        {
            try
            {
                //createPollMessage

                GovTalkGateway PollingService = new GovTalkGateway();
                GovTalkMessage PollMessage =
                    PollingService.CreatePollMessage(correlationId);

                //send Poll Message

                log.Info("Sending poll msg ...");

                string PollURI = CRCOStringFunctions.SetURI(_env, "Poll");

                XmlDocument XmlResponse = new XmlDocument();

                if (_diskPolling)
                {
                    GiftAidSubmissionProcessController.SerializeObjectToFile(PollMessage, PollMsgFileName);
                    XmlResponse = GiftAidSubmissionProcessController.DespatchToGateway(PollMsgFileName, PollURI);
                }
                else
                {
                    XmlDocument PollDoc = GiftAidSubmissionProcessController.SerializeInMemory(PollMessage);
                    XmlResponse = GiftAidSubmissionProcessController.DespatchToGateway(PollDoc, PollURI);
                }
                //readPollAcknowledgement
                log.Info("Reading poll reply msg ...");
                GiftAidSubmissionProcessController.ReadResponse(XmlResponse);

                //write results to file
                System.IO.File.WriteAllText(PollMsgResults + correlationId + ".xml", XmlResponse.InnerXml);

            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
        }
        /// <summary>
        /// Reads Government Gateway Xml responses
        /// </summary>
        /// <param name="xmlresp"></param>
        public static void ReadResponse(XmlDocument xmlresp)
        {
            HMRCDataTable ResponseTable = new HMRCDataTable();

            try
            {
                GovTalkGateway ResponseReader = new GovTalkGateway();
                GovTalkMessage Message = new GovTalkMessage();
                Message = ResponseReader.ReadAcknowledgementAndResponse(xmlresp);

               if (String.IsNullOrEmpty(_correlationId))
                    _correlationId = ResponseReader.CorrelationId;

                _resp = ResponseReader.GatewayResponse;

                // Set the poll URI
                PollURI = Message.Header.MessageDetails.ResponseEndPoint.Value;

                if (_resp == "error")
                {
                    ResponseTable.AddErrorResponseColumns();
                    ResponseTable.AddErrorResponseRow(Message);
                }

                if (ResponseReader.PollInterval > 0)
                    _pollInterval = ResponseReader.PollInterval;

                log.Info(String.Format("Response received and read. Response type is {0}. CorrelationID is {1}", _resp, String.IsNullOrEmpty(_correlationId) ? "Nothing": _correlationId));

                if (ResponseReader.SuccessResponse != null)
                {
                    string message = "";
                    for (int i = 0; i < ResponseReader.SuccessResponse.Message.Length; i++)
                    {
                        message += "Message " + (i+1).ToString() + ": Code = "
                            +ResponseReader.SuccessResponse.Message[i].code.ToString() + "; "
                            + ResponseReader.SuccessResponse.Message[i].Value.ToString() + "; \n";
                    }

                    string FinalMessage = String.Format("Success Response with IRreceipt: {0}, Accepted time: {1}, Message Code: {2}, Message body: {3}",
                        ResponseReader.SuccessResponse.IRmarkReceipt.Message.Value.ToString(), ResponseReader.SuccessResponse.AcceptedTime,
                        ResponseReader.SuccessResponse.IRmarkReceipt.Message.code,
                        message
                        );
                    log.Info(FinalMessage);
                }

                // DataResponse case
                if (Message.Body.StatusReport != null)
                {
                    ResponseTable.AddGatewayColumns();
                    ResponseTable.AddStatusReportColumns();
                    ResponseTable.AddStatusReportRow(Message);
                }

                Data = ResponseTable.GetTable();
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
        }
        /// <summary>
        /// Creates and sends messages to the poll URI
        /// </summary>
        public static void PollService()
        {
            try
            {
                string PollTime = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss", CultureInfo.InvariantCulture);
                string PollMsgFileName = ConfigurationManager.AppSettings["TempFolder"].ToString() + @"\TestPollMessage" + PollTime + ".xml";
                string PollMsgResults = ConfigurationManager.AppSettings["TempFolder"].ToString() + @"\PollMessageResults" + PollTime + ".xml";

                //createPollMessage

                GovTalkGateway PollingService = new GovTalkGateway();
                GovTalkMessage PollMessage =
                    PollingService.CreatePollMessage(_correlationId);

                //send Poll Message

                log.Info("Sending poll msg ...");

                string PollURI = String.IsNullOrEmpty(_pollURI) ? CRCOStringFunctions.SetURI(_env, "Poll"): _pollURI;

                XmlDocument XmlResponse = new XmlDocument();

                if (_diskPolling)
                {
                    GiftAidSubmissionProcessController.SerializeObjectToFile(PollMessage, PollMsgFileName);
                    log.Debug("Poll URI Is " + PollURI);
                    XmlResponse = GiftAidSubmissionProcessController.DespatchToGateway(PollMsgFileName, PollURI);
                }
                else
                {
                    XmlDocument PollDoc = GiftAidSubmissionProcessController.SerializeInMemory(PollMessage);
                    log.Debug("Poll URI Is " + PollURI);
                    XmlResponse = GiftAidSubmissionProcessController.DespatchToGateway(PollDoc, PollURI);
                }

                //readPollAcknowledgement
                log.Info("Reading poll reply msg ...");
                GiftAidSubmissionProcessController.ReadResponse(XmlResponse);

                _counter++;
                log.Info("Counter is now: " + _counter.ToString());

                if (_resp == "response" && !Object.ReferenceEquals(null,_timer))
                {
                    _timer.Stop();
                    _timer.Dispose();
                    _returnValue = _correlationId + "_" + _env + "_" + _resp;
                    System.IO.File.WriteAllText(_resultFileFirstPart + _returnValue + ".xml", XmlResponse.InnerXml);
                    return;
                }

                if (_resp == "error" && !Object.ReferenceEquals(null, _timer))
                {
                    _timer.Stop();
                    _timer.Dispose();
                    _returnValue = _correlationId + "_" + _env + "_" + _resp;
                    System.IO.File.WriteAllText(_resultFileFirstPart + _returnValue + ".xml", XmlResponse.InnerXml);
                    return;
                }

                System.IO.File.WriteAllText(PollMsgResults + _returnValue + ".xml", XmlResponse.InnerXml);
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
        }
        /// <summary>
        /// Creates and sends delete requests to the submission URI
        /// </summary>
        /// <param name="correlationId"></param>
        public static void DeleteRequest(string correlationId, string deleteMsgFileName = "", string deleteMsgResults = "")
        {
            try
            {
                string DeleteTime = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss", CultureInfo.InvariantCulture);
                string DeleteMsgFileName = deleteMsgFileName == "" ? ConfigurationManager.AppSettings["TempFolder"].ToString() + @"\TestDeleteMessage" + DeleteTime + ".xml" : deleteMsgFileName;
                string DeleteMsgResults = deleteMsgResults == "" ? ConfigurationManager.AppSettings["TempFolder"].ToString() + @"\DeleteMessageResults_" + DeleteTime + "_" : deleteMsgResults;

                GovTalkGateway DeleteSubmission = new GovTalkGateway();
                GovTalkMessage DeleteMessage =
                    DeleteSubmission.CreateDeleteRequest(correlationId);
                GiftAidSubmissionProcessController.SerializeObjectToFile(DeleteMessage, DeleteMsgFileName);

                //send Delete Message

                log.Info("Sending delete msg ..");
                string DeleteURI = CRCOStringFunctions.SetURI(_env, "Send");

                XmlDocument XmlResponse = GiftAidSubmissionProcessController.DespatchToGateway(DeleteMsgFileName, DeleteURI);

                //readPollAcknowledgement
                log.Info("Reading delete reply msg ...");
                GiftAidSubmissionProcessController.ReadResponse(XmlResponse);

                if (_resp == "response" && !Object.ReferenceEquals(null, _timer))
                {
                    _timer.Stop();
                    _timer.Dispose();
                    _returnValue = _correlationId + "_" + _env + "_" + _resp;
                    System.IO.File.WriteAllText(DeleteMsgResults+"_"+_correlationId+"_DeleteResponse.xml", XmlResponse.InnerXml);
                    return;
                }

                if (_resp == "error" && !Object.ReferenceEquals(null, _timer))
                {
                    _timer.Stop();
                    _timer.Dispose();
                    _returnValue = _correlationId + "_" + _env + "_" + _resp;
                    System.IO.File.WriteAllText(DeleteMsgResults+"_"+_correlationId+"_DeleteError.xml", XmlResponse.InnerXml);
                    return;
                }

                System.IO.File.WriteAllText(DeleteMsgResults + _returnValue + ".xml", XmlResponse.InnerXml);

            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
            }
        }
        /// <summary>
        /// Creates and sends data requests to the submission URI
        /// </summary>
        public static XmlDocument DataRequest(string DataRequestMsgFileName, string DataRequestMsgResults)
        {
            try
            {
                GovTalkGateway DataRequestSubmission = new GovTalkGateway();
                GovTalkMessage DataRequestMessage =
                    DataRequestSubmission.CreateDataRequest();
                GiftAidSubmissionProcessController.SerializeObjectToFile(DataRequestMessage, DataRequestMsgFileName);

                log.Debug("Sending data request message ... ");
                string DataRequestURI = CRCOStringFunctions.SetURI(_env, "Send");

                if (String.IsNullOrEmpty(DataRequestURI))
                {
                    Exception URIError = new Exception("The Data Request URI is null or empty. Check environment");
                    throw URIError;
                }

                XmlDocument XmlResponse = GiftAidSubmissionProcessController.DespatchToGateway(DataRequestMsgFileName, DataRequestURI);

                System.IO.File.WriteAllText(DataRequestMsgResults, XmlResponse.InnerXml);

                log.Debug("Reading reply msg ...");
                GiftAidSubmissionProcessController.ReadResponse(XmlResponse);

                return XmlResponse;

            }
            catch (Exception ex)
            {
                log.Error(ex.ToString());
                throw ex;
            }
        }