Last active
May 9, 2018 08:27
-
-
Save Axemasta/4436ca6daa79ce523ea1ded7d2616334 to your computer and use it in GitHub Desktop.
CSV Helper Parsing (For http://joshclose.github.io/CsvHelper/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// The class map of the object you want to parse (The csv column headers) | |
/// <summary> | |
sealed class c : ClassMap<ParseUser> | |
{ | |
/// <summary> | |
/// CSV Definition Map For <ParseUser> | |
/// </summary> | |
public ParseUserDefinitionMap() | |
{ | |
Map(m => m.UUID).Name("id"); | |
Map(m => m.Username).Name("username"); | |
Map(m => m.UserType).Name("type"); | |
Map(m => m.IsAdmin).Name("admin"); | |
Map(m => m.DateCreated).Name("joined"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Parsing function | |
/// <summary> | |
public void ReadDocument() | |
{ | |
var filePath = @"C:\\temp\yourfile.csv" //_fileManager.InputFilePath; | |
TextReader textReader = File.OpenText(filePath); | |
var csv = new CsvReader(textReader); | |
csv.Configuration.HasHeaderRecord = true; | |
csv.Configuration.IgnoreQuotes = false; | |
csv.Configuration.RegisterClassMap(new ParseUserDefinitionMap()); | |
var everything = csv.GetRecords<ParseUser>().ToList(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// The object that you want to parse. | |
/// <summary> | |
public class ParseUser | |
{ | |
/// <summary> | |
/// Unique User Id | |
/// </summary> | |
public Guid UUID { get; set; } | |
/// <summary> | |
/// Username | |
/// </summary> | |
public string Username { get; set; } | |
/// <summary> | |
/// User Type | |
/// </summary> | |
public Enum UserType { get; set; } | |
/// <summary> | |
/// Is User Admin? | |
/// </summary> | |
public bool IsAdmin { get; set; } | |
/// <summary> | |
/// Date Account Created | |
/// </summary> | |
public DateTime DateCreated { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment