since this is the first discussion topic, forgive me in advance if I'm wrong. My question will be, I have an example of a CSV file in the form of '.txt' and I am using this file.
- Name
- Address
- Direction
- Status
- Duration
- Date
I want to get values like . I used CSVHelper, either I couldn't do it or the formats I wanted were not in that package. Thanks in advance to those who will help. Attached is the file I want to parse;
Name,Address,Direction,Status,Duration,Date Berat Bey Dörtsan,"05315554622",Outgoing,Unanswered,00:00,00:00,8/4/2022 (9:25:48 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,00:47,00:47,8/4/2022 (9:27:55 AM) Berat Bey Dörtsan,"05315554622",Incoming,Answered,00:54,00:54,8/4/2022 (9:35:02 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,00:19,00:19,8/4/2022 (2:58:43 PM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,00:49,00:49,8/5/2022 (9:21:52 AM) Berat Bey Dörtsan,"05315554622",Incoming,Answered,01:56,01:56,8/16/2022 (10:17:55 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,00:47,00:47,9/7/2022 (11:02:33 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,01:19,01:19,9/7/2022 (11:04:35 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,01:57,01:57,9/7/2022 (11:07:20 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,04:59,04:59,9/7/2022 (11:12:54 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,01:50,01:50,9/7/2022 (11:18:36 AM) Berat Bey Dörtsan,"05315554622",Incoming,Answered,00:37,00:37,9/7/2022 (11:36:36 AM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,00:59,00:59,9/7/2022 (11:55:19 AM) Berat Bey Dörtsan,"05315554622",Incoming,Missed,00:00,00:00,9/7/2022 (12:15:26 PM) Berat Bey Dörtsan,"05315554622",Outgoing,Unanswered,00:00,00:00,9/7/2022 (12:21:12 PM) Berat Bey Dörtsan,"05315554622",Outgoing,Unanswered,00:00,00:00,9/7/2022 (12:21:24 PM) Berat Bey Dörtsan,"05315554622",Outgoing,Answered,00:18,00:18,9/7/2022 (12:36:08 PM)
i tried to do it this way;
- Name = Berat Bey Dörtsan
- Address = 05315554466
- Direction = Outgoing
- Status = Unanswered
- Duration = 00:54 - 01:56
- Date = 8/4/2022 (9:25:48 AM)
and i was used these codes;
using CsvHelper;
using CsvHelper.Configuration.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TextVeriCekme
{
public partial class Form1 : Form
{
String csvPath = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog
{
Title = "CSV Dosyası Aç",
Filter = "csv files (*.csv)|*.csv",
CheckFileExists = true,
CheckPathExists = true
};
if (ofd.ShowDialog() == DialogResult.OK)
{
MessageBox.Show(ofd.FileName);
csvPath = ofd.FileName;
}
}
public class Foo
{
public string Name { get; set; }
public string Address { get; set; }
public string Direction { get; set; }
public string Status { get; set; }
public string Duration { get; set; }
[Name("Date")]
[Format("dd-MM-yyyy")]
public DateTime Date { get; set; }
}
int sayac = 0;
private void button2_Click(object sender, EventArgs e)
{
IEnumerable<Foo> records = null;
List<Foo> list = null;
using (var reader = new StreamReader(csvPath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
records = csv.GetRecords<Foo>();
list = records.ToList();
MessageBox.Show("Toplam Kayıt : " records.Count().ToString());
foreach (Foo record in list)
{
if (sayac == 1)
{
MessageBox.Show(record.Name);
MessageBox.Show(record.Address);
MessageBox.Show(record.Direction);
MessageBox.Show(record.Status);
MessageBox.Show(record.Duration);
MessageBox.Show(record.Date.ToString());
}
sayac ;
}
}
}
}
}
this way I can't get 'Date' variable. what i want to do in general is a windows form to keep these call logs (date and time).
CodePudding user response:
The format decoration for the date in the Foo
type is wrong. The date values look like this:
M/d/yyyy (h:mm:ss tt)
So you need to decorate the type accordingly:
public class Foo
{
public string Name { get; set; }
public string Address { get; set; }
public string Direction { get; set; }
public string Status { get; set; }
public string Duration { get; set; }
[Name("Date")]
[Format("M/d/yyyy (h:mm:ss tt)")]
public DateTime Date { get; set; }
}
If you are not able to change the attribute, you will instead need to provide a ClassMap configuration as demonstrated here:
CodePudding user response:
Part of the problem is that Duration
is two columns in your data, since it is not enclosed in double quotes.
00:47,00:47
instead of"00:47,00:47"
.
As such, you can't go by the header names and instead are going to need to use a ClassMap
to map properties by Index
.
void Main()
{
using (var reader = new StreamReader(@"C:\Temp\CsvFileParsing.txt"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Context.RegisterClassMap<FooMap>();
var records = csv.GetRecords<Foo>().ToList();
}
}
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Map(x => x.Name).Index(0);
Map(x => x.Address).Index(1);
Map(x => x.Direction).Index(2);
Map(x => x.Status).Index(3);
Map(x => x.Duration).Convert(args => args.Row.GetField(4) " - " args.Row.GetField(5));
Map(x => x.Date).Index(6).TypeConverterOption.Format("M/d/yyyy (h:mm:ss tt)");
}
}
public class Foo
{
public string Name { get; set; }
public string Address { get; set; }
public string Direction { get; set; }
public string Status { get; set; }
public string Duration { get; set; }
public DateTime Date { get; set; }
}