I have the followings:
Class Pet
public string PetType {get; set;}
public string PetName {get; set;}
public int PetAge {get; set;}
Class Person
public string Name {get; set;}
public int Age {get; set;}
public List<Pet> ListOfPets {get; set;}
Class ViewModel
public ObservableCollection<Person> People {get; set;}
I wish to serialize the People
object into a csv
file.
Can such task be achieved?
I found a CSV Serializer
which can only handle strings.
A csv-file is two-dimensional, you cannot include a third dimension (the pets). You could manually append the pets one by one but you will loose the fixed column count of the csv file
Like this
[Name],[Age],[Pet1Type][Pet1Name],[Pet1Age],[Pet2Type][Pet2Name],[Pet2Age]
[Name],[Age],[Pet1Type][Pet1Name],[Pet1Age]
[Name],[Age],[Pet1Type][Pet1Name],[Pet1Age],[Pet2Type][Pet2Name],[Pet2Age,[Pet3Type][Pet3Name],[Pet3Age]
But it will be harder to consume afterwards.
After the edit it is clear OP is interested in serializing in CSV format so i can suggest to try out CsvMapper .
var csv = new CsvWriter( textWriter );
csv.WriteRecords( list );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.