How to Parse the txt file in c# [closed]

做~自己de王妃 提交于 2019-12-13 09:54:56

问题


Hello I am new to C# and I have to parse the text file formatted which has the data like following.

H1|57535                 |65644474|       243.34
D1|671690160540      |FedEx Gnd   |Ground          |Parcel |06082016
D2|FCREADHCU3     |    10||||||     23.01
H1|57521                 |65642336|       923.31
D1|671690161010      |FedEx Gnd   |Ground          |Parcel |06082016
D2|PS121B         |     1|      0.00
H1|57521                 |65642336|       923.31
D1|671690161031      |FedEx Gnd   |Ground          |Parcel |06082016
D2|PS121B         |     1|      0.00
H1|57521                 |65642336|       923.31
D1|671690161020      |FedEx Gnd   |Ground          |Parcel |06082016
D2|PS121B         |     1|      0.00
snipping                

How can I parse text file in C#. Help Is appreciated.


回答1:


Start with this:

var lines = File.ReadAllLines("<your path/filename>");
var stringBags = lines.Select(l => l.Split('|'));
var objects = stringBags.Select(b => new {Id = b[0], Name = b[1], SomeOtherField = b[2]});

This gives you a way to parse the file, and to project it into some sort of object you can deal with




回答2:


You could do a foreach loop for each line in the file and parse the string...

foreach (string line in File.ReadAllLines("filepath"))


来源:https://stackoverflow.com/questions/37737777/how-to-parse-the-txt-file-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!