/// <summary> /// Parse EDIFACT UNA 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 HeadersUNA ParseEDIFACTUNAHeader( string input, out string dataElementSeparator, out string releaseIndicator, out string componentDataElementSeparator, out string segmentTerminator ) { dataElementSeparator = ""; releaseIndicator = ""; componentDataElementSeparator = ""; segmentTerminator = ""; if (input == null) { return(null); } HeadersUNA _headerUNA = new HeadersUNA(); try { if (input.Length == 9) { // If headers.xsd was changed, change appropriate fields for new values _headerUNA.UNA1 = input[3].ToString(); _headerUNA.UNA2 = input[4].ToString(); _headerUNA.UNA3 = input[5].ToString(); _headerUNA.UNA4 = input[6].ToString(); _headerUNA.UNA5 = input[7].ToString(); _headerUNA.UNA6 = input[8].ToString(); dataElementSeparator = input[4].ToString(); releaseIndicator = input[6].ToString(); componentDataElementSeparator = input[3].ToString(); segmentTerminator = input[8].ToString(); return(_headerUNA); } 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 GS header. Reason: {0}", ex.Message), ex); } }
/// <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); }