首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#中的Swift MT940解析器

C#中的Swift MT940解析器
EN

Stack Overflow用户
提问于 2019-12-28 00:33:03
回答 1查看 2K关注 0票数 1

如何使用C#解析MT940 swift报文?

下面是我必须解析的消息:

代码语言:javascript
复制
:20:MT940-1411201901
:25:1234567837710016 
:28C:008/201
:60F:C171224SAR145597,13
:61:2107221722D17000,NCHK219120//14218-102431Abnamb
:61:2107221722D17000,NCHK219120//14218-102431Abnamb VSP
:62F:C291124SAR145597,13

我怎么才能意识到这一点呢?

EN

回答 1

Stack Overflow用户

发布于 2019-12-28 07:30:17

尝试下面这样的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            MT940 mt940 = new MT940(FILENAME);
        }
    }
    public class MT940
    {
        const string TAG_PATTERN = @"^:(?'tag'[^:]+):(?'value'.*)";

        public string senderReference { get; set; }  //code 20
        public string authorisation { get; set; } // tag 25
        public string messageIndexTotal { get; set; } //tag 28D
        public Currency openingBalance { get; set; }  //60F
        public string firstTransaction { get; set; } //61
        public string secondTransaction { get; set; } //61
        public Currency closingBalance { get; set; } //62F

        public MT940(string filename)
        {
            StreamReader reader = new StreamReader(filename);
            string line = "";
            int transactionCount = 0;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith(":"))
                {
                    Match match = Regex.Match(line, TAG_PATTERN);
                    string tag = match.Groups["tag"].Value;
                    string value = match.Groups["value"].Value;

                    switch (tag)
                    {
                        case "20":
                            senderReference = value;
                            break;

                        case "25":
                            authorisation = value;
                            break;

                        case "28C":
                            messageIndexTotal = value;
                            break;

                        case "60F":
                            openingBalance = new Currency(value);
                            break;
                        case "61":
                            if (++transactionCount == 1)
                            {
                                firstTransaction = value;
                            }
                            else
                            {
                                secondTransaction = value;
                            }
                            break;

                        case "62F":
                            closingBalance = new Currency(value);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

    }
    public class Currency
    {
        const string BALANCE_PATTERN = @"^(?'credit_debit'.)(?'date'.{6})(?'country_code'.{3})(?'amount'.*)";
        static CultureInfo culture = CultureInfo.GetCultureInfoByIetfLanguageTag("da");

        public DateTime date { get; set; }
        public string currencyCode { get; set; }
        public decimal amount { get; set; }

        public Currency(string input)
        {
            Match match = Regex.Match(input, BALANCE_PATTERN);

            string credit_debit = match.Groups["credit_debit"].Value;

            string dateStr = match.Groups["date"].Value;
            date = DateTime.ParseExact(dateStr, "yyMMdd", CultureInfo.InvariantCulture);

            currencyCode = match.Groups["country_code"].Value;

            string amountStr = match.Groups["amount"].Value;
            amount = decimal.Parse(amountStr, culture);
            amount *= credit_debit == "D" ? -1 : 1;
        }
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59503513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档