使用 С# CSVHELPER 从 CSV 获取值时,代码重复
The code is duplicated when getting values from CSV with С# CSVHELPER
读取CSV时方法中代码重复。如何删除重复项以及为什么会出现重复项?写入 CSV 时没有它们,但在读取时,重复项开始:
方法:
public class Program
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Product(int id, string name)
{
Id = id;
Name = name;
}
}
public static void Main(string[] args)
{
Research();
}
public static void Research()
{
using (var writer = new StreamWriter("C:\Users\Saint\Desktop\TaskRetail\file.csv", false, Encoding.UTF8))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(products);
}
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ",",
PrepareHeaderForMatch = header => header.Header.ToLower()
};
using (var reader = new StreamReader("C:\Users\Saint\Desktop\TaskRetail\file.csv", Encoding.UTF8))
using (var csv = new CsvReader(reader, config))
{
var records = csv.GetRecords<Product>();
foreach (var record in records)
{
Console.WriteLine($"{record.Id} {record.Name}");
}
}
}
}
}
输出到控制台:
问题是您在创建产品的循环中包含了 CSV 文件的写入和读取。您的第一个产品已创建,它被写入 CSV 文件,然后被读取并打印到您的控制台。循环继续并创建您的第二个产品。产品 1 和产品 2 被写入 CSV 文件,然后它们都从 CSV 文件中读取并打印到您的控制台。这对创建的每个产品都会继续,这就是您在控制台中显示重复项的原因。在写入 file.csv
之前,您需要关闭 for
循环
for (var i = 0; i < count; i++)
{
var element = xmlDoc.Item(i);
var id = int.Parse(element.Attributes.GetNamedItem("id").Value);
var name = element.SelectSingleNode("name").InnerText;
var product = new Product(id, name);
//Console.WriteLine($"Id: {id}, name: {name}");
products.Add(product);
} // Close your loop here!!
using (var writer = new StreamWriter("C:\Users\Saint\Desktop\TaskRetail\file.csv", false, Encoding.UTF8))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(products);
}
读取CSV时方法中代码重复。如何删除重复项以及为什么会出现重复项?写入 CSV 时没有它们,但在读取时,重复项开始:
方法:
public class Program
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Product(int id, string name)
{
Id = id;
Name = name;
}
}
public static void Main(string[] args)
{
Research();
}
public static void Research()
{
using (var writer = new StreamWriter("C:\Users\Saint\Desktop\TaskRetail\file.csv", false, Encoding.UTF8))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(products);
}
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = ",",
PrepareHeaderForMatch = header => header.Header.ToLower()
};
using (var reader = new StreamReader("C:\Users\Saint\Desktop\TaskRetail\file.csv", Encoding.UTF8))
using (var csv = new CsvReader(reader, config))
{
var records = csv.GetRecords<Product>();
foreach (var record in records)
{
Console.WriteLine($"{record.Id} {record.Name}");
}
}
}
}
}
输出到控制台:
问题是您在创建产品的循环中包含了 CSV 文件的写入和读取。您的第一个产品已创建,它被写入 CSV 文件,然后被读取并打印到您的控制台。循环继续并创建您的第二个产品。产品 1 和产品 2 被写入 CSV 文件,然后它们都从 CSV 文件中读取并打印到您的控制台。这对创建的每个产品都会继续,这就是您在控制台中显示重复项的原因。在写入 file.csv
for
循环
for (var i = 0; i < count; i++)
{
var element = xmlDoc.Item(i);
var id = int.Parse(element.Attributes.GetNamedItem("id").Value);
var name = element.SelectSingleNode("name").InnerText;
var product = new Product(id, name);
//Console.WriteLine($"Id: {id}, name: {name}");
products.Add(product);
} // Close your loop here!!
using (var writer = new StreamWriter("C:\Users\Saint\Desktop\TaskRetail\file.csv", false, Encoding.UTF8))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(products);
}