public XmlNodeClass XmlRead(string Path) { //存放配置文件各节点数据 XmlNodeClass nodeC = new XmlNodeClass(); //加载xml文件 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Path); XmlNode RootNode = xmlDoc.SelectSingleNode("configuration"); XmlNodeList NodeList = RootNode.ChildNodes; #region 遍历获取配置文件各节点数据 foreach (XmlNode node in NodeList) { if (node.NodeType == XmlNodeType.Element) { switch (node.Name) { case "FormalDataOffset": nodeC.FormalDataOffset = Convert.ToInt32(node.InnerXml); break; case "HeaderOffset": nodeC.HeaderOffset = Convert.ToInt32(node.InnerXml); break; case "DataLocation": nodeC.DataLocation = node.InnerXml; break; case "DGIGroup": nodeC.DGIGroup = node.InnerXml; break; default: break; } } } #endregion return nodeC; }
private void button4_Click(object sender, EventArgs e) { string startuppath = System.Windows.Forms.Application.StartupPath; string ConfigPath = startuppath + "\\" + "config.xml"; #region 获取配置文件内容 XmlNodeClass nodeclass = new XmlNodeClass(); try { XmlHelperClass xmlhelper = new XmlHelperClass(); nodeclass = xmlhelper.XmlRead(ConfigPath); } catch (Exception ex) { throw ex; } #endregion #region 根据配置文件内容分割DGI字符串 List<DataFormat> DataBaseList = new List<DataFormat>(); string[] DataBases = nodeclass.DGIGroup.Split(':'); foreach (string database in DataBases) { string[] format = database.Split('|'); DataFormat dataformat = new DataFormat() { DGIName = format[0], BeforeData = Convert.ToInt64(format[1]), DataLen = Convert.ToInt32(format[2]) }; DataBaseList.Add(dataformat); } #endregion //string path = @"C:\Users\Administrator\Desktop\新建文件夹\用完即删\光大测试\20151212-71216-47384.out"; //string path = @"C:\Users\Administrator\Desktop\新建文件夹\吉林银行正式卡\吉林银行正式数据\8406-PBOCY0120020000001A-151208-001-02-1.DP"; string path = @"C:\Users\Administrator\Desktop\新建文件夹\吉林银行正式卡\旧数据\8406-PBOCY0120020000001A-150616-002-02-0.DP"; FileStream fs = File.OpenRead(path); BinaryReader DataReader = new BinaryReader(fs); //获取数据开始的位置,数组大小由配置文件获取 byte[] FileByte = new byte[nodeclass.HeaderOffset]; DataReader.Read(FileByte, 0, nodeclass.HeaderOffset); string str = BitConverter.ToString(FileByte).Replace("-", ""); int StartIndex = str.IndexOf(nodeclass.DataLocation); //数据开始的位置 int DataStartIndex = StartIndex / 2 + nodeclass.DataLocation.Length / 2; List<string> DataString = new List<string>(); StringBuilder DGIData = new StringBuilder(); //将二进制流的位置从流的开头偏移DataStartIndex个字节,忽略文件头的内容,直接到需要截取的数据 DataReader.BaseStream.Seek(DataStartIndex, SeekOrigin.Begin); while (DataReader.BaseStream.Position != DataReader.BaseStream.Length) { //跳过每条数据前不需要获取的字节 if (nodeclass.FormalDataOffset > 0) { DataReader.BaseStream.Seek(nodeclass.FormalDataOffset, SeekOrigin.Current); } //清空可变字符字符串DGIData的内容 DGIData.Clear(); for (int i = 0; i < DataBaseList.Count; i++) { DataFormat DGIDataFormat = DataBaseList[i]; string DGIDataStr = string.Empty; if (DGIDataFormat.DGIName == "0301") { //跳过86 DataReader.BaseStream.Seek(1, SeekOrigin.Current); byte[] CAFlagByte = new byte[1]; DataReader.Read(CAFlagByte, 0, 1); string CAFlagStr = BitConverter.ToString(CAFlagByte).Replace("-", ""); switch (CAFlagStr) { case "82": DGIDataStr = GetDataStr(ref DataReader, 2); DGIData.Append(DGIDataFormat.DGIName + "|" + (DGIDataStr.Length / 2).ToString("X4") + DGIDataStr + "|"); break; case "81": DGIDataStr = GetDataStr(ref DataReader, 1); DGIData.Append(DGIDataFormat.DGIName + "|" + (DGIDataStr.Length / 2).ToString("X2") + DGIDataStr + "|"); break; }; } else { //如果有86的话,数据流需要往前进一位 DataReader.BaseStream.Seek(DGIDataFormat.BeforeData, SeekOrigin.Current); DGIDataStr = GetDataStr(ref DataReader, DGIDataFormat.DataLen); //获取数据长度字节的个数,一个字节对应两位数字,如0301DGI数据长度为0101,两个字节对应四位数字 string format = "X" + (DGIDataFormat.DataLen * 2).ToString(); switch (DGIDataFormat.DGIName) { case "DF51": string DF51Str = "0101" + DGIDataStr; DGIData.Append(DGIDataFormat.DGIName + "|" + (DF51Str.Length / 2).ToString("X2") + DF51Str + "|"); break; case "DF52": string DF52 = "A50C880101" + DGIDataStr; string DF52Str = "9102" + (DF52.Length / 2).ToString("X2") + DF52; DGIData.Append(DGIDataFormat.DGIName + "|" + (DF52Str.Length / 2).ToString("X2") + DF52Str + "|"); break; case "DF53": string DF53Str = "9102" + DGIDataStr; DGIData.Append(DGIDataFormat.DGIName + "|" + (DF53Str.Length / 2).ToString("X2") + DF53Str + "|"); break; default: DGIData.Append(DGIDataFormat.DGIName + "|" + (DGIDataStr.Length / 2).ToString(format) + DGIDataStr + "|"); break; } } } DataString.Add(DGIData.ToString()); } }