/// <summary>
        /// This method serializes EDIFACT headers UNA, UNB and UNG to an XML document, so each segment is an element and is wrapped around header elements
        /// </summary>
        /// <param name="stringHeaderUNA">String with a UNA segment</param>
        /// <param name="stringHeaderUNB">String with a UNA segment</param>
        /// <param name="stringHeaderUNG">String with a UNA segment</param>
        /// <returns>XmlDocument representation of a UNA, UNB and UNG headers</returns>
        public static XmlDocument SerializeEDIFACTHeaders(string stringHeaderUNA, string stringHeaderUNB, string stringHeaderUNG)
        {
            HeadersUNA headerUNA = null;
            HeadersUNB headerUNB = null;
            HeadersUNG headerUNG = null;

            string DataElementSeparator          = string.Empty;
            string ReleaseIndicator              = string.Empty;
            string ComponentDataElementSeparator = string.Empty;
            string SegmentSeparator              = string.Empty;

            if (!string.IsNullOrEmpty(stringHeaderUNA))
            {
                headerUNA = MessageEnrichmentLibrary.EDIHeaders.ParseEDIFACTUNAHeader(
                    stringHeaderUNA,
                    out DataElementSeparator,
                    out ReleaseIndicator,
                    out ComponentDataElementSeparator,
                    out SegmentSeparator
                    );
            }
            else
            {
                headerUNA      = new HeadersUNA();
                headerUNA.UNA2 = "+";
                headerUNA.UNA4 = "?";
                headerUNA.UNA1 = ":";
                headerUNA.UNA6 = "'";
                headerUNA.UNA3 = ",";
                headerUNA.UNA5 = "*";

                DataElementSeparator          = headerUNA.UNA2;
                ReleaseIndicator              = headerUNA.UNA4;
                ComponentDataElementSeparator = headerUNA.UNA1;
                SegmentSeparator              = headerUNA.UNA6;
            }

            headerUNB = MessageEnrichmentLibrary.EDIHeaders.ParseEDIFACTUNBHeader(
                stringHeaderUNB,
                DataElementSeparator,
                ReleaseIndicator,
                ComponentDataElementSeparator,
                SegmentSeparator
                );

            if (!string.IsNullOrEmpty(stringHeaderUNG))
            {
                headerUNG = MessageEnrichmentLibrary.EDIHeaders.ParseEDIFACTUNGHeader(
                    stringHeaderUNG,
                    DataElementSeparator,
                    ReleaseIndicator,
                    ComponentDataElementSeparator,
                    SegmentSeparator
                    );
            }

            // Skip xml declaration for a newly created documents
            StringBuilder     _newXML   = new StringBuilder();
            XmlWriterSettings _settings = new XmlWriterSettings();

            _settings.OmitXmlDeclaration = true;

            // Compose result document
            XmlWriter _writer = XmlWriter.Create(_newXML, _settings);

            _writer.WriteStartElement(_headersPrefix, _headersName, _headersNamespace);
            _writer.WriteRaw(XmlToString(Serialize(headerUNA)));
            _writer.WriteRaw(XmlToString(Serialize(headerUNB)));
            if (headerUNG != null)
            {
                _writer.WriteRaw(XmlToString(Serialize(headerUNG)));
            }
            _writer.WriteEndElement();
            _writer.Flush();

            XmlDocument _newDocument = new XmlDocument();

            _newDocument.LoadXml(_newXML.ToString());
            return(_newDocument);
        }
        /// <summary>
        /// Parse EDIFACT UNB header
        /// </summary>
        /// <param name="input">Input string</param>
        /// <param name="dataElementSeparator">Data element separator</param>
        /// <param name="releaseIndicator">Release indicator</param>
        /// <param name="componentDataElementSeparator">Component data element separator</param>
        /// <param name="segmentTerminator">Segment terminator</param>
        /// <returns>XML document with structure from headers.xsd EDIFACT UNA header description</returns>
        public static HeadersUNB ParseEDIFACTUNBHeader(
            string input,
            string dataElementSeparator,
            string releaseIndicator,
            string componentDataElementSeparator,
            string segmentTerminator
            )
        {
            if (input == null)
            {
                return(null);
            }

            HeadersUNB _UNBHeader = new HeadersUNB();

            Regex _UNBRegex = new Regex(CreateParsePattern(_UNBGenericPattern, dataElementSeparator, releaseIndicator, componentDataElementSeparator, segmentTerminator));
            Match _UNBMatch = _UNBRegex.Match(input);

            string _escapedReleaseIndicator = EscapeRegexCharacter(releaseIndicator);

            try
            {
                if (_UNBMatch != null)
                {
                    // If headers.xsd was changed, change appropriate fields for new values
                    _UNBHeader.UNB1       = new HeadersUNBUNB1();
                    _UNBHeader.UNB1.UNB11 = _UNBMatch.Groups["SyntaxIdentifier"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB1.UNB12 = _UNBMatch.Groups["SyntaxVersionNumber"].ToString();
                    _UNBHeader.UNB2       = new HeadersUNBUNB2();
                    _UNBHeader.UNB2.UNB21 = _UNBMatch.Groups["SenderIdentification"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB2.UNB22 = _UNBMatch.Groups["SenderIdentificationCodeQualifier"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB2.UNB23 = _UNBMatch.Groups["AddressForReverseRouting"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB3       = new HeadersUNBUNB3();
                    _UNBHeader.UNB3.UNB31 = _UNBMatch.Groups["RecipientIdentification"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB3.UNB32 = _UNBMatch.Groups["RecipientIdentificationCodeQualifier"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB3.UNB33 = _UNBMatch.Groups["RoutingAddress"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB4       = new HeadersUNBUNB4();
                    _UNBHeader.UNB4.UNB41 = _UNBMatch.Groups["Date"].ToString();
                    _UNBHeader.UNB4.UNB42 = _UNBMatch.Groups["Time"].ToString();
                    _UNBHeader.UNB5       = _UNBMatch.Groups["InterchangeControlReference"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB6       = new HeadersUNBUNB6();
                    _UNBHeader.UNB6.UNB61 = _UNBMatch.Groups["RecipientPassword"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB6.UNB62 = _UNBMatch.Groups["RecipientPasswordQualifier"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB7       = _UNBMatch.Groups["ApplicationReference"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB8       = _UNBMatch.Groups["ProcessingPriorityCode"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB9       = _UNBMatch.Groups["AcknowledgementRequest"].ToString();
                    _UNBHeader.UNB10      = _UNBMatch.Groups["CommunicationsAgreementId"].ToString().Replace(_escapedReleaseIndicator, String.Empty);
                    _UNBHeader.UNB11      = _UNBMatch.Groups["TestIndicator"].ToString();

                    return(_UNBHeader);
                }
                else
                {
                    throw new ApplicationException("Can't parse a message. Not enough parsing elements found.");
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(string.Format("Failed to parse UNB header. Reason: {0}", ex.Message), ex);
            }
        }