【问题标题】:Reading an MT940 file data and storing into database table读取 MT940 文件数据并存入数据库表
【发布时间】:2021-11-17 01:35:00
【问题描述】:

我是 MT940 文件的新手,我正在寻找一个示例代码来读取 .mt940 的内容并将其存储在具有相应字段的数据库表中。我正在努力分析它。 有什么简单的方法可以解析并保存在表格中吗?

以下面一行为例(这不是整个 mt940 只是一行)

:61:2009230923D4086,74NDDTNONREF //NONREF

如何从上述行中检索客户参考 (=NONREF )?它不会出现在所有行的相同索引中。有时它从 28index 开始,有时从 30index 开始。

【问题讨论】:

标签: c# xml parsing datatable mt940


【解决方案1】:

使用正则表达式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        const string pattern = @":61:(?'ValueDate'.{6})(?'EntryDate'.{4})(?'Mark'.{2})(?'FundCode'.{1})(?'Amount'[\d,]+)(?'ID'.{4})((?'CustomerReference'.{1,16})(//)(?'BankReference'.*)|(?'CustomerReference'.{1,16}))";
        static void Main(string[] args)
        {
            string data = File.ReadAllText(FILENAME);
            MatchCollection matches = Regex.Matches(data, pattern, RegexOptions.Multiline);

            foreach (Match match in matches)
            {

                string valueDate = match.Groups["ValueDate"].Value;
                string entryDate = match.Groups["EntryDate"].Value;
                string mark = match.Groups["Mark"].Value;
                string fundCode = match.Groups["FundCode"].Value;
                string amount = match.Groups["Amount"].Value;
                string id = match.Groups["ID"].Value;
                string customerReference = match.Groups["CustomerReference"].Value;
                string bankReference = match.Groups["BankReference"].Value;
                Console.WriteLine("ValueDate = {0}, EntryDate = {1}, Mark = {2}, FundCode = {3}, Amount = {4}, ID = {5}, Customer = {6}, Bank = {7}",
                    valueDate, entryDate, mark, fundCode, amount, id, customerReference, bankReference);
            }
            Console.ReadLine();

        }
    }
}

【讨论】:

  • 如何将“Mark”和“FundCode”字段设为可选?有时我在这两个字段中没有数据。
  • 该字段是可选的,可以使用或不使用该字段。我更新了代码以使用匹配集合获取所有 61 个字段
  • 我得到的金额是 86,74,但正确的答案应该是 4086,74。这意味着标记和基金代码正在选择一些金额数字
  • 我使用的是我在上面发布的第二个链接,上面写着:6!n[4!n]2a[1!a]15d1!a3!c16x[//16x] 数字应该是86,74.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 2019-05-17
  • 1970-01-01
相关资源
最近更新 更多