C# 解析文本文件中的块数据并将其映射到实体

C# 解析文本文件中的块数据并将其映射到实体,c#,parsing,asp.net-core,C#,Parsing,Asp.net Core,我有一个文本文件,其中包含如下神数据: AA Origin: Egypt According to the Book Of The Dead, herald at the door of the Third Arit AABIT Origin: Egypt A goddess of song, voice, music and art 我希望解析此数据并将其映射到具有以下属性的神实体: public class Deity { public int Id { get; set; }

我有一个文本文件,其中包含如下神数据:

AA
Origin: Egypt
According to the Book Of The Dead, herald at the door of the Third Arit

AABIT
Origin: Egypt
A goddess of song, voice, music and art
我希望解析此数据并将其映射到具有以下属性的神实体:

public class Deity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Origin { get; set; }
    public string Description { get; set; }
}
其目的是使用EF Core将数据从
.txt
移动到某种类型的关系数据库

当我在Notepad++中打开文件并显示行尾时,我看到:

我以前没有写过解析器,我也见过很多处理逐行解析文本文件的示例,但我不确定这将如何处理,因为我的是逐块解析,而不是逐行解析


有没有办法在.NET核心控制台应用程序中逐块解析?

我假设
Id
将在插入数据库时自动生成

以下是解析的逻辑步骤:

var deities = new List<Deity>();
var lines = File.ReadAllLines("Your file.txt");
for(int i; i < lines.Count; i++)
{
    var line = lines[i];

    // Get the chunk of lines which represents a deity
    var objectLines = new List<string>();
    while(!string.IsNullOrWhiteSpace(line)) // check if there is some other character for the blank line, use it here if needed
    {
        objectLines.Add(line);
        line = lines[++i];
        continue;
    }

    // Create the deity object using that chunk
    var deity = new Deity 
    {
        Name = objectLines[0],
        Origin = objectLines[1], // You might have to remove the prefix Origin :
        Description  = objectLines[2],
    };
    deities.Add(deity);
}
var神=新列表();
var lines=File.ReadAllLines(“Your File.txt”);
for(int i;i

这是没有测试,但应该让你的神名单

我假设
Id
将在插入数据库时自动生成

以下是解析的逻辑步骤:

var deities = new List<Deity>();
var lines = File.ReadAllLines("Your file.txt");
for(int i; i < lines.Count; i++)
{
    var line = lines[i];

    // Get the chunk of lines which represents a deity
    var objectLines = new List<string>();
    while(!string.IsNullOrWhiteSpace(line)) // check if there is some other character for the blank line, use it here if needed
    {
        objectLines.Add(line);
        line = lines[++i];
        continue;
    }

    // Create the deity object using that chunk
    var deity = new Deity 
    {
        Name = objectLines[0],
        Origin = objectLines[1], // You might have to remove the prefix Origin :
        Description  = objectLines[2],
    };
    deities.Add(deity);
}
var神=新列表();
var lines=File.ReadAllLines(“Your File.txt”);
for(int i;i

这是没有测试,但应该让你的神名单

您可以使用读取文本文件。使用空行和每个条目的预期行数作为我们必须解析下一个神的指示符

此代码假设每个神只使用三行(即描述中没有换行符)。它将跳过文件开头、条目内部、条目之间以及文件末尾的任何空行

using System.Collections.Generic;
using System.IO;

public static IEnumerable<Deity> Parse(string filePath) {

    var deities = new List<Deity>();
    var currentDeity = new Deity();
    var currentFieldIndex = 0;

    foreach (string line in File.ReadLines(filePath)) {

        // empty line might indicate next deity
        if (string.IsNullOrWhiteSpace(line)) {
            // next deity only if all fields of current deity have been found yet
            if (currentFieldIndex > 2) {
                deities.Add(currentDeity);
                currentDeity = new Deity();
                currentFieldIndex = 0;
            }
            continue;
        }

        // at this point we know that the line is not empty
        switch(currentFieldIndex) {
            case 0: currentDeity.Name = line; currentFieldIndex++; break;
            case 1: currentDeity.Origin = line.Replace("Origin: ", string.Empty); currentFieldIndex++; break;
            case 2: currentDeity.Description = line; currentFieldIndex++; break;
            default: throw new ArgumentException("Expected 3 lines per entry.");
        }
    }

    return deities;
}
使用System.Collections.Generic;
使用System.IO;
公共静态IEnumerable解析(字符串文件路径){
var Deties=新列表();
var currentDeity=新神();
var currentFieldIndex=0;
foreach(File.ReadLines(filePath)中的字符串行){
//空行可能表示下一位神
if(string.IsNullOrWhiteSpace(行)){
//下一个神祗在当前神祗的所有领域都被发现的情况下
如果(currentFieldIndex>2){
神。添加(当前神);
当前神=新神();
currentFieldIndex=0;
}
继续;
}
//此时,我们知道该行不是空的
开关(currentFieldIndex){
案例0:CurrentDedity.Name=line;currentFieldIndex++;break;
案例1:CurrentDedity.Origin=line.Replace(“Origin:,string.Empty”);currentFieldIndex++;break;
案例2:CurrentDedity.Description=line;currentFieldIndex++;break;
默认值:抛出新的ArgumentException(“预期每个条目3行”);
}
}
回归神;
}

.

您可以使用读取文本文件。使用空行和每个条目的预期行数作为我们必须解析下一个神的指示符

此代码假设每个神只使用三行(即描述中没有换行符)。它将跳过文件开头、条目内部、条目之间以及文件末尾的任何空行

using System.Collections.Generic;
using System.IO;

public static IEnumerable<Deity> Parse(string filePath) {

    var deities = new List<Deity>();
    var currentDeity = new Deity();
    var currentFieldIndex = 0;

    foreach (string line in File.ReadLines(filePath)) {

        // empty line might indicate next deity
        if (string.IsNullOrWhiteSpace(line)) {
            // next deity only if all fields of current deity have been found yet
            if (currentFieldIndex > 2) {
                deities.Add(currentDeity);
                currentDeity = new Deity();
                currentFieldIndex = 0;
            }
            continue;
        }

        // at this point we know that the line is not empty
        switch(currentFieldIndex) {
            case 0: currentDeity.Name = line; currentFieldIndex++; break;
            case 1: currentDeity.Origin = line.Replace("Origin: ", string.Empty); currentFieldIndex++; break;
            case 2: currentDeity.Description = line; currentFieldIndex++; break;
            default: throw new ArgumentException("Expected 3 lines per entry.");
        }
    }

    return deities;
}
使用System.Collections.Generic;
使用System.IO;
公共静态IEnumerable解析(字符串文件路径){
var Deties=新列表();
var currentDeity=新神();
var currentFieldIndex=0;
foreach(File.ReadLines(filePath)中的字符串行){
//空行可能表示下一位神
if(string.IsNullOrWhiteSpace(行)){
//下一个神祗在当前神祗的所有领域都被发现的情况下
如果(currentFieldIndex>2){
神。添加(当前神);
当前神=新神();
currentFieldIndex=0;
}
继续;
}
//此时,我们知道该行不是空的
开关(currentFieldIndex){
案例0:CurrentDedity.Name=line;currentFieldIndex++;break;
案例1:CurrentDedity.Origin=line.Replace(“Origin:,string.Empty”);currentFieldIndex++;break;
案例2:CurrentDedity.Description=line;currentFieldIndex++;break;
默认值:抛出新的ArgumentException(“预期每个条目3行”);
}
}
回归神;
}

.

我想你可以试试这个

假设有多行描述,并且记录结束有一个空行。另外,您所描述的文件

using (var sr = new StreamReader(fileName))
   while (!sr.EndOfStream)
   {
      var diety = new Deity();
      diety.Name = sr.ReadLine();
      diety.Origin = sr.ReadLine().Replace("Origin: ", string.Empty);

      string val;
      while (!string.IsNullOrWhiteSpace(val = sr.ReadLine()))
         diety.Description += val;

      deities.Add(diety);
   }

我想你可以试试这个

假设有多行描述,并且记录结束有一个空行。另外,您所描述的文件

using (var sr = new StreamReader(fileName))
   while (!sr.EndOfStream)
   {
      var diety = new Deity();
      diety.Name = sr.ReadLine();
      diety.Origin = sr.ReadLine().Replace("Origin: ", string.Empty);

      string val;
      while (!string.IsNullOrWhiteSpace(val = sr.ReadLine()))
         diety.Description += val;

      deities.Add(diety);
   }
什么是
AABIT