public string SetDocument(string xml, string sender, string receiver, string fasti2sender, string fasti2receiver, string cspProfile, string documentType)
    {
        OracleCredentials oracleCredentials = new OracleCredentials();

        oracleCredentials.Database = settings.DatabaseServer;
        oracleCredentials.User     = settings.DatabaseUsername;
        oracleCredentials.Password = settings.DatabaseUserPassword;

        OracleConnectionClass oracle = new OracleConnectionClass(oracleCredentials);

        Logger.setOracleConnector(oracle);

        settings.CspProfile = cspProfile;

        string result = "0";

        string      id = "";
        string      file = "";
        string      error = "";
        XmlDocument signed = null;
        string      reference = "", confirmation = "";
        int         errorCode = 0;

        try
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(xml);
            signed = SignDocument(document, sender, receiver);

            id     = signed.DocumentElement["SignedData"]["Data"]["Root"]["DocumentUniqueIdentifier"].InnerText;
            file   = documentType + id + ".xml";
            result = "1";

            signed.Save(settings.OutXmlDir + file);
        }
        catch (ArgumentException)
        {
            error = "Неверный формат XML файла";
            Logger.Add(LoggerEventsType.ERROR, "Неверный формат XML файла " + id);
        }
        catch (Exception ex)
        {
            error = ex.Message;
            Logger.Add(ex);
        }

        if (!String.IsNullOrEmpty(error))
        {
            errorCode = -1;
        }

        if (errorCode != -1)
        {
            error = fasti2.SendMessage(fasti2sender, fasti2receiver, settings.CspProfile,
                                       /* Ref */ out reference,
                                       /* Conf */ out confirmation);

            if (!String.IsNullOrEmpty(error))
            {
                errorCode = -1;
            }
        }

        XDocument answer = new XDocument(
            new XDeclaration("1.0", Encoding.GetEncoding("UTF-8").HeaderName, "yes"),
            new XElement("root",
                         new XElement("Result", result),
                         new XElement("DocumentUniqueIdentifier", id),
                         new XElement("FileName", file),
                         new XElement("Reference", reference),
                         new XElement("Confirmation", confirmation),
                         new XElement("Error", error),
                         new XElement("ErrorCode", errorCode),
                         new XElement("SignedXml", XElement.Parse(signed.OuterXml))
                         )
            );

        return(answer.ToString());
    }
    public String GetAnswers(string receiver, string cspProfile)
    {
        string file_name_err = "";

        try
        {
            OracleCredentials oracleCredentials = new OracleCredentials();
            oracleCredentials.Database = settings.DatabaseServer;
            oracleCredentials.User     = settings.DatabaseUsername;
            oracleCredentials.Password = settings.DatabaseUserPassword;

            OracleConnectionClass oracle = new OracleConnectionClass(oracleCredentials);

            Logger.setOracleConnector(oracle);

            Logger.Add(LoggerEventsType.UNKNOWN, "receiver = " + receiver + "\r\ncspProfile = " + cspProfile);

            fasti2.ReceiveMessages(receiver, cspProfile);

            var nonXmlFiles = Array.FindAll(Directory.GetFiles(settings.InXmlDir), x => !x.EndsWith(".xml"));
            foreach (var file in nonXmlFiles)
            {
                string new_file_name = "", eng_file_name = "", new_eng_file_name = "";
                try
                {
                    eng_file_name = new_eng_file_name = oracle.Transliterate(Path.GetFileName(file));
                    if (!File.Exists(settings.WebServerKfmFilesDir + eng_file_name) && !File.Exists(settings.InNonXmlDir + Path.GetFileName(file)))
                    {
                        new_file_name = Path.GetFileName(file);
                        File.Copy(file, settings.WebServerKfmFilesDir + eng_file_name);
                        File.Move(file, settings.InNonXmlDir + Path.GetFileName(file));
                    }
                    else
                    {
                        int j = 1;
                        while (true)
                        {
                            new_file_name     = j.ToString() + "_" + Path.GetFileName(file);
                            new_eng_file_name = j.ToString() + "_" + eng_file_name;
                            if (!File.Exists(settings.WebServerKfmFilesDir + new_eng_file_name) && !File.Exists(settings.InNonXmlDir + new_file_name))
                            {
                                File.Copy(file, settings.WebServerKfmFilesDir + new_eng_file_name);
                                File.Move(file, settings.InNonXmlDir + new_file_name);
                                break;
                            }
                            j++;
                        }
                    }
                    oracle.SaveKfmFilename(new_file_name + "/" + new_eng_file_name, File.GetLastWriteTime(settings.InNonXmlDir + new_file_name));
                    Logger.Add(LoggerEventsType.DEBUG, "File " + new_file_name + " successfully moved to NoXmlFilesDir and copied to web-server. Eng Name: " + new_eng_file_name);
                }
                catch (Exception ex)
                {
                    Logger.Add(LoggerEventsType.ERROR, "Try to copy and move file. Initial name: " + file + ". Final name: " + new_file_name + ". Eng Name: " + new_eng_file_name + ". " + ex.ToString());
                }
            }

            var files = Directory.GetFiles(settings.InXmlDir, "*.xml");

            XmlDocument document = new XmlDocument();
            foreach (var file in files)
            {
                file_name_err = Path.GetFileName(file);
                document.Load(file);
                var    status = SignatureValidateStatus.Valid; // Verify(document);
                string originalid;
                string version;
                string id;
                try
                {
                    originalid = document.DocumentElement["SignedData"]["Data"]["Check"]["OriginalDocumentGuid"].InnerText;
                    version    = document.DocumentElement["SignedData"]["Data"]["Check"]["Version"].InnerText;
                    id         = document.DocumentElement["SignedData"]["Data"]["Check"]["DocumentUniqueIdentifier"].InnerText;
                }
                catch (Exception ex)
                {
                    Logger.Add(ex);
                    Logger.Add(LoggerEventsType.ERROR, "Не возможно получить доступ к объектам:");

                    try
                    {
                        originalid = document.DocumentElement["SignedData"]["Data"]["Root"]["OriginalDocumentGuid"].InnerText;
                        version    = document.DocumentElement["SignedData"]["Data"]["Root"]["Version"].InnerText;
                        id         = document.DocumentElement["SignedData"]["Data"]["Root"]["DocumentUniqueIdentifier"].InnerText;
                    }
                    catch (Exception e)
                    {
                        Logger.Add(e);
                        Logger.Add(LoggerEventsType.ERROR, "Не возможно получить доступ к объектам:");
                        Logger.Add(LoggerEventsType.ERROR, "Файл " + file + " некоректный.");
                        continue;
                    }
                }

                decimal errorcode = -1;
                string  error;

                if (status == SignatureValidateStatus.Valid)
                {
                    try
                    {
                        try
                        {
                            errorcode = decimal.Parse(document.DocumentElement["SignedData"]["Data"]["Check"]["ErrorCode"].InnerText);
                            error     = document.DocumentElement["SignedData"]["Data"]["Check"]["ErrorName"].InnerText;
                        }
                        catch
                        {
                            errorcode = decimal.Parse(document.DocumentElement["SignedData"]["Data"]["Root"]["ErrorCode"].InnerText);
                            error     = document.DocumentElement["SignedData"]["Data"]["Root"]["ErrorName"].InnerText;
                        }
                        oracle.SaveReceiveMessage(id, errorcode, error, document.OuterXml, id, version, originalid);
                    }
                    catch (Exception e)
                    {
                        Logger.Add(e);

                        string description = "";
                        try
                        {
                            description  = document.DocumentElement["SignedData"]["Data"]["Root"]["Description"].InnerText;
                            description += " Количество дней на ответ: " + document.DocumentElement["SignedData"]["Data"]["Root"]["CountDays"].InnerText;
                        }
                        finally
                        {
                            oracle.SaveReceiveMessage(id, 0, description, document.OuterXml, id, version, originalid);
                        }
                    }
                }
                else
                {
                    oracle.SaveReceiveMessage(id, -1 * (int)status, Enum.GetName(typeof(SignatureValidateStatus), status), document.OuterXml, id, version, originalid);
                }

                try
                {
                    File.Move(file, fasti2.GetArchiveDayFolder(settings.InXmlDirArchive) + Path.GetFileName(file));
                }
                catch (Exception ex)
                {
                    File.Delete(file);
                    Logger.Add(ex);
                }
            }
        }
        catch (Exception e)
        {
            Logger.Add(e);
            if (File.Exists(settings.InXmlErrDir + file_name_err))
            {
                File.Move(settings.InXmlDir + file_name_err, settings.InXmlErrDir + "exists_" + file_name_err);
            }
            else
            {
                File.Move(settings.InXmlDir + file_name_err, settings.InXmlErrDir + file_name_err);
            }
            return(e.Message + "\nИмя некорректного файла: " + file_name_err);
        }

        return("");
    }