private static void ParseHeaders <T>(XmlNodeList nodes, MessageFactory <T> mfact) where T : IsoMessage { ArrayList <XmlElement> refs = null; for (var i = 0; i < nodes.Count; i++) { var elem = (XmlElement)nodes.Item(i); if (elem != null) { var type = ParseType(elem.GetAttribute("type")); if (type == -1) { throw new IOException($"Invalid type {elem.GetAttribute("type")} for ISO8583 header: "); } if (elem.ChildNodes == null || elem.ChildNodes.Count == 0) { if (elem.GetAttribute("ref") != null && !string.IsNullOrEmpty(elem.GetAttribute("ref"))) { if (refs == null) { refs = new ArrayList <XmlElement>(nodes.Count - i); } refs.Add(elem); } else { throw new IOException("Invalid ISO8583 header element"); } } else { var header = elem.ChildNodes.Item(0).Value; var binHeader = "true".Equals(elem.GetAttribute("binary")); if (logger.IsEnabled(LogEventLevel.Debug)) { var binary = binHeader ? "binary" : string.Empty; logger.Debug( $"Adding {binary} ISO8583 header for type {elem.GetAttribute("type")} : {header}"); } if (binHeader) { mfact.SetBinaryIsoHeader(type, HexCodec.HexDecode(header).ToUnsignedBytes()); } else { mfact.SetIsoHeader(type, header); } } } } if (refs == null) { return; } { foreach (var elem in refs) { if (elem == null) { continue; } var type = ParseType(elem.GetAttribute("type")); if (type == -1) { throw new IOException("Invalid type for ISO8583 header: " + elem.GetAttribute("type")); } if (elem.GetAttribute("ref") == null || elem.GetAttribute("ref").IsEmpty()) { continue; } var t2 = ParseType(elem.GetAttribute("ref")); if (t2 == -1) { throw new IOException("Invalid type reference " + elem.GetAttribute("ref") + " for ISO8583 header " + type); } var h = mfact.GetIsoHeader(t2); if (h == null) { throw new ArgumentException("Header def " + type + " refers to nonexistent header " + t2); } if (logger.IsEnabled(LogEventLevel.Debug)) { logger.Debug("Adding ISO8583 header for type {Type}: {H} (copied from {Ref})", elem.GetAttribute("type"), h, elem.GetAttribute("ref")); } mfact.SetIsoHeader(type, h); } } }