从文件读取而没有特定索引

时间:2018-10-10 08:27:59

标签: c#

没有人知道如何读取.txt文件吗?如果没有指定具体的编号来读取这些行多少次。例如:
FileName.txt

ShopName;Adress;TNumber;  
ItemName;Price;Color;Weight;Size;Rating;  
ItemName;Price;Color;Weight;Size;Rating;  
ItemName;Price;Color;Weight;Size;Rating;  
ItemName;Price;Color;Weight;Size;Rating;  
ShopName;Adress;TNumber;  
ItemName;Price;Color;Weight;Size;Rating;  
ItemName;Price;Color;Weight;Size;Rating;  
ShopName;Adress;TNumber;   
ItemName;Price;Color;Weight;Size;Rating;  
ItemName;Price;Color;Weight;Size;Rating;  
ItemName;Price;Color;Weight;Size;Rating;  

ShopName,Adress,TNumber转到一个类Shop,该类(Shop.cs)存储来自另一个类Item.cs的数据

1 个答案:

答案 0 :(得分:1)

检查线型的唯一机会是对分号进行计数(或者拆分并删除结果数组长度)。

public static void Main()
{
    StreamReader sr = new StreamReader(@"C:\Temp\Test.txt");
    List<Shop> shops = new List<Shop>();
    Shop shop = null;
    while(!sr.EndOfStream)
    {
        string line = sr.ReadLine().Trim().TrimEnd(';');

        string[] split = line.Split(';');

        if (split.Count() == 3)
        {
            if (shop != null)
                shops.Add(shop);

            shop = new Shop()
            {
                Name = split[0],
                Adr = split[1],
                TNumber = split[2],
            };
        }
        else
        {
            Item item = new Item()
            {
                Name = split[0],
                Price = split[1],
                Color = split[2],
                Weight = split[3],
                Size = split[4],
                Rating = split[5]
            };
            shop.Items.Add(item);
        }
    }
    if (shop != null)
        shops.Add(shop);

    foreach (Shop s in shops)
    {
        Console.WriteLine(s.Name + ", " + s.Adr + ", " + s.TNumber);
        foreach (Item i in s.Items)
        {
            Console.WriteLine("   " + i.Name + ", " + i.Price);
        }
    }
}

public class Shop
{
    public Shop() { this.Items = new List<Item>(); }
    public string Name { get; set; }
    public string Adr { get; set; }
    public string TNumber { get; set; }

    public List<Item> Items { get; set; }
}

public class Item
{
    public string Name { get; set; }
    public string Price { get; set; }
    public string Color { get; set; }
    public string Weight { get; set; }
    public string Size { get; set; }
    public string Rating { get; set; }
}