从文本文件ant中读取文本并分离为变量

时间:2020-08-31 12:59:35

标签: c# list variables streamreader

我正在尝试为混乱文本中的其他变量设置值。

主菜 名称;组;价格; TAX;编号; Id ;;全名;描述;

更大的修饰符 名称;组;价格;税;号码; Id; /一些/其他/东西

更小的修饰符 名称;价格;税;编号; Id; /一些/其他/东西

Text 

Omlet;Second Dishes;40,0000;;00027;326ef70c-8d29-4c63-94ce-0580f26f84ab;Omlet with chicken and mushroom sauce;;;;;
Onions;Vegetables;4,1000;21;00021;fe5bab77-72cf-474e-acbc-1562c2f6aa37;0/1/1/1/6;;;;
Tomatoes;Vegetables;4,2000;21;00022;180fa908-9428-444e-a1df-5b74a40def64;0/1/1/1/7;;;;
Day Soup;Soup;123,4560;9;10108;19674f89-a44a-423d-ae79-0fc020be8d72;;;;;;
Roast pork with sauce;Second Dishes;0,0500;21;1167;a929bf86-2b89-4af6-baf9-f37317e0d75f;;;;;;
Cucumbers;;0,5500;21;222;8e370b64-b1f8-4665-95ae-88327d877394;-/-/1/0/3;;;;;
Tomatoes with garlic;Vegetables;0,1100;21;00024;52d08882-41c2-4dc3-8c4b-998109b6aedc;-/-/1/0/3;;;;;
Salt;;0,3300;21;00025;39332fab-99e0-4663-a59a-fff0deab958d;-/-/1/0/3;;;;;

我创建了一个课程

    class Food
    {
        public string Name;
        public string Group;
        public string Price;
        public string TaxPercent;
        public string Number;
        public string ID;
        public string Type;
        public string FullName;
        public string Description;
}

有人可以向我解释我如何将零件分为较小的修饰符和较大的修饰符吗?我应该创建一个新的类,还是可以与一个人一起工作? IF语句应如何显示?

到目前为止我一直在尝试

 private void button1_Click(object sender, EventArgs e)
        {
            List<Food> List= new List<Food>();
            using (StreamReader sr = new StreamReader(@"TEST.txt"))
            {
                while (!sr.EndOfStream)
                
                {
                    string str;
                    string[] stringArray;
                    str = sr.ReadLine();

                    strArray = str.Split(';');
                    Food Dish = new Food();
                    Dish.Name = stringArray[0];
                    Dish.Group = stringArray[1];
                    Dish.Price = stringArray[2];
                    Dish.TaxPercent = stringArray[3];

                    List.Add(Dish);

                    textBox1.Text =displayMembers(List); 
    


} 
string displayMembers(List<Food> vegetables)
            {
                foreach (Food s in vegetables)
                {
                    return s.ToString();
                }
                return null;
          

 Out put 

TESTREADER.FOOD  //TESTREADER IS A FOLDER WHERE TEST FILE IS.

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

public List<Food> getFoods()
    {
        List<Food> foods = new List<Food>();
        using (var fileReader = new StreamReader(@"file.txt"))
        {
            var line = fileReader.ReadLine();
            while (line != null)
            {
                string[] data = s.Split(';');
                line = fileReader.ReadLine();
                Food food = new Food
                {
                    Name = data[0];
                    Group = data[1];
                    (...)
            }

            line = fileReader.ReadLine();
        }

        return foods;
    }
相关问题