.NET中固定宽度处理的最佳实践 [英] Best practices for fixed-width processing in .NET

查看:91
本文介绍了.NET中固定宽度处理的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET Web服务,该服务将处理具有相对较长的多级记录格式的文本文件.文件中的每个记录代表一个不同的实体;该记录包含多个子类型. (如果可以使您更好地了解我们正在查看的内容,则COBOL作业当前正在处理相同的记录格式).我创建了一个类结构(如果需要的话,可以使用DATA DIVISION)来保存输入数据.

I'm working a .NET web service that will be processing a text file with a relatively long, multilevel record format. Each record in the file represents a different entity; the record contains multiple sub-types. (The same record format is currently being processed by a COBOL job, if that gives you a better picture of what we're looking at). I've created a class structure (a DATA DIVISION if you will) to hold the input data.

我的问题是,您发现在.NET中处理大型,复杂的固定宽度文件的最佳实践是什么?我的一般方法是将整行读入字符串,然后将字符串中的数据解析为我创建的类.但是我不确定使用字符串中的字符作为数组还是使用字符串本身是否可以获得更好的结果.我想这是一个具体的问题,字符串与char [],但我希望任何人都拥有其他任何指针.

My question is, what best practices have you found for processing large, complex fixed-width files in .NET? My general approach will be to read the entire line into a string and then parse the data from the string into the classes I've created. But I'm not sure whether I'll get better results working with the characters in the string as an array, or with the string itself. I guess that's the specific question, string vs. char[], but I would appreciate any other pointers anyone has.

谢谢.

推荐答案

我将使用类型,长度等属性构建与行中数据匹配的类.然后使用Microsoft.VisualBasic.FileIO.TextFieldParser对象读取文件,并使用一些通用代码,用于根据类对解析器进行编程,然后读取数据并创建类的实例(全部使用反射).

I would build classes that matched the data in the rows, using attributes for types, length etc. Then use the Microsoft.VisualBasic.FileIO.TextFieldParser object for reading the file, with some generic code for programming the parser based on the class, then reading the data and creating an instance of the class (all using reflection).

我用它来读取CSV及其快速,灵活,可扩展,通用并且易于维护.我还具有一些属性,可让我在读取每个字段时向其添加通用验证.

I use this for reading CSVs and its fast, flexible, extenisble, generic and easy to maintain. I also have attributes that allow me to add generic validation to each field as its being read.

我会分享我的代码,但是它是我所工作的公司的IP.

I'd share my code, but its the IP of the firm I work for.

这篇关于.NET中固定宽度处理的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆