当前位置: 首页>>代码示例>>C#>>正文


C# CsvReader.GetRecord方法代码示例

本文整理汇总了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>();


        }
开发者ID:adwardliu,项目名称:vc-community,代码行数:29,代码来源:ImportExportTest.cs

示例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;

            }
        }
开发者ID:jcteague,项目名称:OOP-Training,代码行数:29,代码来源:ProceduralActorImporter.cs

示例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"] );

			}
		}
开发者ID:Core2D,项目名称:CsvHelper,代码行数:27,代码来源:ExceptionMessageTests.cs

示例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"));
     }
 }
开发者ID:ssarabando,项目名称:WebApi.Html5.Upload,代码行数:29,代码来源:UploadingController.cs

示例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;
        }
开发者ID:oysteinkrog,项目名称:thing_libutils,代码行数:55,代码来源:NutMetricHex.cs

示例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 );
        }
开发者ID:rdingwall,项目名称:CsvHelper,代码行数:54,代码来源:ReferenceMappingAttributeTests.cs

示例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 );
 }
开发者ID:KPK-Teamwork-Team-Bajic,项目名称:KPK-Teamwork,代码行数:12,代码来源:EnumerableConverterTests.cs

示例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;
        }
开发者ID:danielgardiner81,项目名称:CustomerRisk,代码行数:14,代码来源:BetRepository.cs

示例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 );
        }
开发者ID:kharnt0x,项目名称:CsvHelper,代码行数:49,代码来源:ReferenceMappingAttributeTests.cs

示例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 );
        }
开发者ID:sethwebster,项目名称:CsvHelper,代码行数:48,代码来源:ReferenceMappingAttributeTests.cs

示例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 )
			{
			}
		}
开发者ID:jhamm,项目名称:CsvHelper,代码行数:18,代码来源:EnumerableConverterTests.cs

示例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);
		}
开发者ID:adwardliu,项目名称:vc-community,代码行数:44,代码来源:CsvCatalogImporter.cs

示例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);
                        }
                    }
                }
            }
        }
开发者ID:earlosse,项目名称:ATimeOfWar,代码行数:19,代码来源:CsvSeeder.cs

示例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;
        }
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:19,代码来源:Program.cs

示例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);
        }
开发者ID:amandamc4,项目名称:bti420winter2015,代码行数:42,代码来源:Manager.cs


注:本文中的CsvReader.GetRecord方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。