将txt文件解析为List

时间:2017-05-13 07:17:45

标签: c#

我有一个想要存储到数组中的txt文件。当我在单行上有其他项目时,我遇到了解析问题,因为管道(|)将指示另一个要存储的项目。

Inv #     Date     term qty description         price Tax
3221409:2017/01/12:215|10:WD2002:2TB Hard Drive:121.66:N|20:KG240S:240GB SSD:125.10:N|20:KG120S:120GB SSD:78.75:N

我试图首先尝试打开文件并将每个元素显示到控制台。获取索引超出数组的范围。该文件没有我提供的标题以供参考。  //此对象允许您从文件中读取。             StreamReader streamReader = null;

        string lineData;
        string[] lineElements;

        if (File.Exists(path))
        {
            Console.WriteLine("Woohoo file found");
            try
            {
                int invoice;
                String invoicedate;
                int term;
                int qty;
                string description;
                Boolean tax;


                streamReader = new StreamReader(path);
                while (streamReader.Peek() > 0)
                {
                    lineData = streamReader.ReadLine();
                    var parts = lineData.Split('|');
                    lineElements = parts.First().Split(':');                        
                    invoice = int.Parse(lineElements[0]);
                    invoicedate = (lineElements[1]);
                    term = int.Parse(lineElements[2]);
                    qty = int.Parse(lineElements[3]);
                    Console.WriteLine(invoice);
                    Console.WriteLine(invoicedate);
                    Console.WriteLine(term);
                    Console.WriteLine(qty);



                }

1 个答案:

答案 0 :(得分:0)

首先定义一个可以保存信息的结构

public class InventoryData
{
    public string Inv { get; set; }
    public DateTime Date { get; set; }
    public string Term { get; set; }
    public IList<InventoryArticle> Articles { get; set; }
}

public class InventoryArticle
{
    public int Quantity { get; set; }
    public string Whatever { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public bool Tax { get; set; }
}

然后是两个方法,它们将一行解析为结构

static InventoryData ParseInventoryData( string data )
{
    var parts = data.Split( '|' );
    var headparts = parts.First().Split( ':' );
    var result = new InventoryData
    {
        Inv = headparts[0],
        Date = DateTime.ParseExact( headparts[1], "yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture ),
        Term = headparts[2],
        Articles = parts.Skip( 1 ).Select( part => ParseInventoryArticle( part ) ).ToList(),
    };
    return result;
}

static InventoryArticle ParseInventoryArticle( string data )
{
    var parts = data.Split( ':' );
    var result = new InventoryArticle
    {
        Quantity = int.Parse( parts[0] ),
        Whatever = parts[1],
        Description = parts[2],
        Price = decimal.Parse( parts[3], System.Globalization.CultureInfo.InvariantCulture ),
        Tax = parts[4] == "Y",
    };
    return result;
}

最后如何解析文件的内容

string[] content = {
    "Inv #     Date     term qty description         price Tax",
    "3221409:2017/01/12:215|10:WD2002:2TB Hard Drive:121.66:N|20:KG240S:240GB SSD:125.10:N|20:KG120S:120GB SSD:78.75:N" };

var data = content
    // skip the header row
    .Skip( 1 ) 
    // parse the content
    .Select( row => ParseInventoryData( row ) ) 
    .ToList();