简体   繁体   English

如何在C#中使用反射读取平面文件的架构

[英]How to Read Schema of Flat File using Reflection in C#

How to Read Schema of Flat File using Reflection in C# The brief overview that I have is- There's some server which is storing the data as flat files and I have to push data from SQL Server to these flat files. 如何在C#中使用反射读取平面文件的架构我的简要概述是-有一些服务器将数据存储为平面文件,我必须将数据从SQL Server推送到这些平面文件。 For that I need to know their schema. 为此,我需要了解他们的架构。 I have no idea about this, any help would be great. 我对此一无所知,任何帮助都会很棒。

Hard to know where to start with this question, because it sounds like reflection is unlikely to be helpful, and yet you specified that it must appear in the solution! 很难知道从哪里开始这个问题,因为听起来像反射不太可能有帮助,但您已指定它必须出现在解决方案中! :) :)

As a result, this answer is mostly guesswork and will probably evolve as you reveal more information. 结果,该答案大部分是猜测,可能会随着您揭示更多信息而演变。 This is fine; 这可以; you can't get the answer (42) until you know what the question is. 在您知道问题所在之前,您无法获得答案(42)。

There isn't a single definition of what a flat file is. 对于平面文件没有唯一的定义。 What you probably need is a function that parses the "records" in the file. 您可能需要的是一个解析文件中“记录”的函数。 The usual approach is to open the file in a hex editor and infer the format from a few example records. 通常的方法是在十六进制编辑器中打开文件,然后从一些示例记录中推断出格式。 Look for clues as to the shape of the repeating pattern. 寻找有关重复图案形状的线索。 Does each "record" appear to consist of a number of "fields"? 每个“记录”是否似乎都由多个“字段”组成? Do they appear in a definite order every time, or is each field prefixed by a name or number to indicate what it means? 它们是否每次都以确定的顺序出现,还是每个字段都以名称或数字作为前缀以表示其含义? Does it look like plain text, ie can you open it in Notepad without losing important information? 它看起来像纯文本吗?也就是说,您可以在记事本中打开它而不会丢失重要信息吗?

Declare a class to represent a record: 声明一个类来代表一条记录:

public class FlatFileRecord
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // etc.
}

Then write a function that reads from a stream and yields records: 然后编写一个从流中读取并产生记录的函数:

public static IEnumerable<FlatFileRecord> ParseFlatFile(StreamReader stream)
{
    // etc.

It would be appropriate to use StreamReader if the file is "plain text" (that is, it has carriage-return/line-feed characters that are used in some significant way to delimit records or fields). 如果文件是“纯文本”(即,它具有回车/换行字符,以某种重要方式用于分隔记录或字段),则使用StreamReader是合适的。

Suppose each record is a line of plain text (in other words, each record is separated by a special line-ending sequence). 假设每个记录是一行纯文本(换句话说,每个记录由特殊的行尾序列分隔)。 Within that line there are multiple fields, somehow delimited by other special characters. 在该行中,存在多个字段,这些字段以某种方式由其他特殊字符分隔。 Your parser might be structured as a loop: 您的解析器可能被构造成一个循环:

string line;
while ((line = r.ReadLine()) != null)
    yield return new FlatFileRecord(line);

This breaks the problem down into two levels. 这将问题分为两个级别。 The outer level goes through the lines one at a time, each producing one record. 外部级别一次通过一行,每行产生一条记录。 Then there is an inner level that I've imagined putting in the constructor of FlatFileRecord. 然后有一个我想象过的内部层次放入FlatFileRecord的构造函数中。 Pass a line, and it fills itself in with the field values that it knows how to extract from that line. 传递一行,并用它知道如何从该行提取的字段值填充自身。 So now you have to write the constructore of FlatFileRecord. 因此,现在您必须编写FlatFileRecord的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM