本文整理汇总了C#中CsvReader.GetRecord方法的典型用法代码示例。如果您正苦于以下问题:C# CsvReader.GetRecord方法的具体用法?C# CsvReader.GetRecord怎么用?C# CsvReader.GetRecord使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CsvReader
的用法示例。
在下文中一共展示了CsvReader.GetRecord方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ImportProductsTest
public void ImportProductsTest()
{
//Auto detect mapping configuration
var importConfiguration = GetMapConfiguration();
var csvProducts = new List<CsvProduct>();
using (var reader = new CsvReader(new StreamReader(@"c:\Projects\VCF\vc-community\PLATFORM\Modules\Catalog\VirtoCommerce.CatalogModule.Test\products.csv")))
{
reader.Configuration.Delimiter = ";";
var initialized = false;
while (reader.Read())
{
if (!initialized)
{
importConfiguration.AutoMap(reader.FieldHeaders);
reader.Configuration.RegisterClassMap(new CsvProductMap(importConfiguration));
initialized = true;
}
var csvProduct = reader.GetRecord<CsvProduct>();
csvProducts.Add(csvProduct);
}
};
var categories = new List<coreModel.Category>();
}
示例2: ImportFile
public void ImportFile(string filename)
{
var actor_repository = new ActorRepository();
if (filename.EndsWith("csv"))
{
var stream = new StreamReader(filename);
var csv_reader = new CsvReader(stream);
while (csv_reader.Read())
{
var actor = csv_reader.GetRecord<Actor>();
actor_repository.Save(actor);
}
return;
}
if (filename.EndsWith("xml"))
{
var stream = new StreamReader(filename);
var serializer = new XmlSerializer(typeof(Actor));
var actors = (Actor[])serializer.Deserialize(stream);
foreach (var actor in actors)
{
actor_repository.Save(actor);
}
return;
}
}
示例3: GetRecordGenericTest
public void GetRecordGenericTest()
{
var parser = new ParserMock
{
{ "Id", "Name" },
{ "a", "b" },
null
};
var reader = new CsvReader( parser );
reader.Read();
try
{
reader.GetRecord<Simple>();
Assert.Fail();
}
catch( CsvTypeConverterException ex )
{
var expected = "Row: '2' (1 based)\r\n" +
"Type: 'CsvHelper.Tests.Exceptions.ExceptionMessageTests+Simple'\r\n" +
"Field Index: '0' (0 based)\r\n" +
"Field Name: 'Id'\r\n" +
"Field Value: 'a'\r\n";
Assert.AreEqual( expected, ex.Data["CsvHelper"] );
}
}
示例4: Post
public Task<HttpResponseMessage> Post()
{
if (Request.Content.IsMimeMultipartContent()) {
var streamProvider = new MultipartMemoryStreamProvider();
var task = Request.Content
.ReadAsMultipartAsync(streamProvider)
.ContinueWith<HttpResponseMessage>(t => {
if (t.IsFaulted || t.IsCanceled) {
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
foreach (var item in streamProvider.Contents) {
var getContentTask = item.ReadAsStreamAsync();
var csvReader = new CsvReader(new StreamReader(getContentTask.Result));
csvReader.Configuration.CultureInfo = CultureInfo.CurrentCulture;
csvReader.Configuration.Delimiter = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
csvReader.Configuration.RegisterClassMap<ReadingMap>();
while (csvReader.Read()) {
Debug.WriteLine(csvReader.GetRecord<Reading>());
}
}
return Request.CreateResponse(HttpStatusCode.OK, new {
Result = "OK"
});
});
return task;
} else {
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
}
示例5: Parse
public static List<INutEntry> Parse(List<ThreadEntry> metricThreadEntries)
{
var entries = new List<INutEntry>();
using (var file = File.OpenText(@"NutMetricHex\iso4032-NutMetricHex.csv"))
{
// skip header
file.ReadLine();
using (var csv = new CsvReader(file))
{
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.CultureInfo = CultureInfo.InvariantCulture;
csv.Configuration.RegisterClassMap(new NutMetricHexEntryMap(metricThreadEntries));
while (csv.Read())
{
try
{
var entry = csv.GetRecord<NutHexEntry>();
entries.Add(entry);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
using (var file = File.OpenText(@"NutMetricHex\iso4035-NutMetricHexThin.csv"))
{
// skip header
file.ReadLine();
using (var csv = new CsvReader(file))
{
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.CultureInfo = CultureInfo.InvariantCulture;
csv.Configuration.RegisterClassMap(new NutMetricHexEntryMap(metricThreadEntries));
while (csv.Read())
{
try
{
var entry = csv.GetRecord<NutHexThinEntry>();
entries.Add(entry);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
return entries;
}
示例6: ReferenceMappingTest
public void ReferenceMappingTest()
{
var count = 0;
var parserMock = new Mock<ICsvParser>();
parserMock.Setup( m => m.Configuration ).Returns( new CsvConfiguration() );
parserMock.Setup( m => m.Read() ).Returns( () =>
{
if( count == 0 )
{
return new[]
{
"FirstName",
"LastName",
"HomeStreet",
"HomeCity",
"HomeState",
"HomeZip",
"WorkStreet",
"WorkCity",
"WorkState",
"WorkZip"
};
}
count++;
return new[]
{
"John",
"Doe",
"1234 Home St",
"Home Town",
"Home State",
"12345",
"5678 Work Rd",
"Work City",
"Work State",
"67890"
};
} );
var reader = new CsvReader( parserMock.Object );
reader.Read();
var person = reader.GetRecord<Person>();
Assert.Equal( "FirstName", person.FirstName );
Assert.Equal( "LastName", person.LastName );
Assert.Equal( "HomeStreet", person.HomeAddress.Street );
Assert.Equal( "HomeCity", person.HomeAddress.City );
Assert.Equal( "HomeState", person.HomeAddress.State );
Assert.Equal( "HomeZip", person.HomeAddress.Zip );
Assert.Equal( "WorkStreet", person.WorkAddress.Street );
Assert.Equal( "WorkCity", person.WorkAddress.City );
Assert.Equal( "WorkState", person.WorkAddress.State );
Assert.Equal( "WorkZip", person.WorkAddress.Zip );
}
示例7: ReadTest
public void ReadTest()
{
var queue = new Queue<string[]>();
queue.Enqueue( new[] { "Id", "Names" } );
queue.Enqueue( new[] { "1", "one" } );
queue.Enqueue( null );
var parserMock = new ParserMock( queue );
var csv = new CsvReader( parserMock );
csv.Read();
var record = csv.GetRecord<Test>();
Assert.IsNull( record.Names );
}
示例8: ReadBets
private IEnumerable<Bet> ReadBets(string resourcesSettledCsv)
{
var bets = new List<Bet>();
using (var reader = _fileSystem.File.OpenText(resourcesSettledCsv))
using (var csvReader = new CsvReader(reader))
{
while (csvReader.Read())
{
bets.Add(csvReader.GetRecord<Bet>());
}
}
return bets;
}
示例9: ReferenceMappingTest
public void ReferenceMappingTest()
{
var queue = new Queue<string[]>();
queue.Enqueue( new[]
{
"FirstName",
"LastName",
"HomeStreet",
"HomeCity",
"HomeState",
"HomeZip",
"WorkStreet",
"WorkCity",
"WorkState",
"WorkZip"
} );
queue.Enqueue( new[]
{
"John",
"Doe",
"1234 Home St",
"Home Town",
"Home State",
"12345",
"5678 Work Rd",
"Work City",
"Work State",
"67890"
} );
queue.Enqueue( null );
var parserMock = new Mock<ICsvParser>();
parserMock.Setup( m => m.Configuration ).Returns( new CsvConfiguration() );
parserMock.Setup( m => m.Read() ).Returns( queue.Dequeue );
var reader = new CsvReader( parserMock.Object );
reader.Read();
var person = reader.GetRecord<Person>();
Assert.Equal( "John", person.FirstName );
Assert.Equal( "Doe", person.LastName );
Assert.Equal( "1234 Home St", person.HomeAddress.Street );
Assert.Equal( "Home Town", person.HomeAddress.City );
Assert.Equal( "Home State", person.HomeAddress.State );
Assert.Equal( "12345", person.HomeAddress.Zip );
Assert.Equal( "5678 Work Rd", person.WorkAddress.Street );
Assert.Equal( "Work City", person.WorkAddress.City );
Assert.Equal( "Work State", person.WorkAddress.State );
Assert.Equal( "67890", person.WorkAddress.Zip );
}
示例10: ReferenceMappingTest
public void ReferenceMappingTest()
{
var queue = new Queue<string[]>();
queue.Enqueue( new[]
{
"FirstName",
"LastName",
"HomeStreet",
"HomeCity",
"HomeState",
"HomeZip",
"WorkStreet",
"WorkCity",
"WorkState",
"WorkZip"
} );
var row = new[]
{
"John",
"Doe",
"1234 Home St",
"Home Town",
"Home State",
"12345",
"5678 Work Rd",
"Work City",
"Work State",
"67890"
};
queue.Enqueue( row );
queue.Enqueue( null );
var parserMock = new ParserMock( queue );
var reader = new CsvReader( parserMock );
reader.Read();
var person = reader.GetRecord<Person>();
Assert.AreEqual( "John", person.FirstName );
Assert.AreEqual( "Doe", person.LastName );
Assert.AreEqual( "1234 Home St", person.HomeAddress.Street );
Assert.AreEqual( "Home Town", person.HomeAddress.City );
Assert.AreEqual( "Home State", person.HomeAddress.State );
Assert.AreEqual( "12345", person.HomeAddress.Zip );
Assert.AreEqual( "5678 Work Rd", person.WorkAddress.Street );
Assert.AreEqual( "Work City", person.WorkAddress.City );
Assert.AreEqual( "Work State", person.WorkAddress.State );
Assert.AreEqual( "67890", person.WorkAddress.Zip );
}
示例11: ReadTest
public void ReadTest()
{
var queue = new Queue<string[]>();
queue.Enqueue( new[] { "Names" } );
queue.Enqueue( new[] { "one" } );
queue.Enqueue( null );
var parserMock = new ParserMock( queue );
var csv = new CsvReader( parserMock );
csv.Read();
try
{
csv.GetRecord<Test>();
Assert.Fail();
}
catch( CsvTypeConverterException )
{
}
}
示例12: DoImport
public void DoImport(Stream inputStream, CsvImportInfo importInfo, Action<ExportImportProgressInfo> progressCallback)
{
var csvProducts = new List<CsvProduct>();
var progressInfo = new ExportImportProgressInfo
{
Description = "Reading products from csv..."
};
progressCallback(progressInfo);
using (var reader = new CsvReader(new StreamReader(inputStream)))
{
reader.Configuration.Delimiter = importInfo.Configuration.Delimiter;
reader.Configuration.RegisterClassMap(new CsvProductMap(importInfo.Configuration));
reader.Configuration.WillThrowOnMissingField = false;
while (reader.Read())
{
try
{
var csvProduct = reader.GetRecord<CsvProduct>();
csvProducts.Add(csvProduct);
}
catch (Exception ex)
{
var error = ex.Message;
if (ex.Data.Contains("CsvHelper"))
{
error += ex.Data["CsvHelper"];
}
progressInfo.Errors.Add(error);
progressCallback(progressInfo);
}
}
}
var catalog = _catalogService.GetById(importInfo.CatalogId);
SaveCategoryTree(catalog, csvProducts, progressInfo, progressCallback);
SaveProducts(catalog, csvProducts, progressInfo, progressCallback);
}
示例13: SeedSingleEntity
public IEnumerable SeedSingleEntity(string resource, Type type)
{
using (var stream = Assembly.GetManifestResourceStream(resource))
{
if (stream != null)
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
var csvReader = new CsvReader(reader);
csvReader.Configuration.WillThrowOnMissingField = false;
while (csvReader.Read())
{
yield return csvReader.GetRecord(type);
}
}
}
}
}
示例14: GetRulePortingInfo
private IEnumerable<PortingInfo> GetRulePortingInfo()
{
var rulesToPort = new List<PortingInfo>();
var reader = new StreamReader(new FileStream(_options.RulesFilePath, System.IO.FileMode.Open));
var csv = new CsvReader(reader);
csv.Configuration.RegisterClassMap<PortingInfoClassMap>();
while (csv.Read())
{
var portingInfo = csv.GetRecord<PortingInfo>();
if (portingInfo.Disposition != Disposition.Unknown)
{
rulesToPort.Add(portingInfo);
}
}
return rulesToPort;
}
示例15: LoadFromCSV
public IEnumerable<QuarterbackBase> LoadFromCSV()
{
// Test if there's data first, and if yes, return the existing data
// In other words, do NOT load the data again
if (ds.Quarterbacks.Count() > 0)
{
var fetchedObjects = ds.Quarterbacks.OrderBy(q => q.Rank);
return Mapper.Map<IEnumerable<QuarterbackBase>>(fetchedObjects);
}
// File system path to the data file (in this project's App_Data folder)
string path = HttpContext.Current.Server.MapPath("~/App_Data/NFLStats2014.csv");
// Create a stream reader object, to read from the file system
StreamReader sr = File.OpenText(path);
// Create the CsvHelper object
var csv = new CsvReader(sr);
// Configure the mapping classes (if it exists)
csv.Configuration.RegisterClassMap<QuarterbackMap>();
// Go through the data file
while (csv.Read())
{
// Read one line in the source file into a new object
QuarterbackAdd qb = csv.GetRecord<QuarterbackAdd>();
// Add the new object to the data store
ds.Quarterbacks.Add(Mapper.Map<Quarterback>(qb));
}
ds.SaveChanges();
// Clean up
sr.Close();
sr = null;
// Return the results
var results = ds.Quarterbacks.OrderBy(q => q.Rank);
return Mapper.Map<IEnumerable<QuarterbackBase>>(results);
}