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


C# CsvReader.TryGetField方法代码示例

本文整理汇总了C#中CsvReader.TryGetField方法的典型用法代码示例。如果您正苦于以下问题:C# CsvReader.TryGetField方法的具体用法?C# CsvReader.TryGetField怎么用?C# CsvReader.TryGetField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CsvReader的用法示例。


在下文中一共展示了CsvReader.TryGetField方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryGetFieldIndexTest

        public void TryGetFieldIndexTest()
        {
            var parserMock = new ParserMock
            {
                { "One", "Two", "Two" },
                { "1", "2", "3" }
            };
            var reader = new CsvReader( parserMock );
            reader.Read();

            int field;
            var got = reader.TryGetField( "Two", 0, out field );
            Assert.IsTrue( got );
            Assert.AreEqual( 2, field );

            got = reader.TryGetField( "Two", 1, out field );
            Assert.IsTrue( got );
            Assert.AreEqual( 3, field );
        }
开发者ID:KPK-Teamwork-Team-Bajic,项目名称:KPK-Teamwork,代码行数:19,代码来源:TryGetTests.cs

示例2: TryGetDoesNotThrowWhenWillThrowOnMissingFieldIsEnabled

        public void TryGetDoesNotThrowWhenWillThrowOnMissingFieldIsEnabled()
        {
            var data = new[] { "1" };
            var queue = new Queue<string[]>();
            queue.Enqueue( data );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Configuration.WillThrowOnMissingField = true;
            reader.Read();
            string field;
            Assert.IsFalse( reader.TryGetField( "test", out field ) );
        }
开发者ID:KPK-Teamwork-Team-Bajic,项目名称:KPK-Teamwork,代码行数:14,代码来源:TryGetTests.cs

示例3: TryGetFieldInvalidNameTest

		public void TryGetFieldInvalidNameTest()
		{
			var data1 = new[] { "One", "Two" };
			var data2 = new[] { "one", "two" };
			var queue = new Queue<string[]>();
			queue.Enqueue( data1 );
			queue.Enqueue( data2 );
			queue.Enqueue( null );
			var parserMock = new ParserMock( queue );

			var reader = new CsvReader( parserMock );
			reader.Read();

			int field;
			var got = reader.TryGetField( "One", out field );
			Assert.IsFalse( got );
			Assert.AreEqual( default( int ), field );
		}
开发者ID:Core2D,项目名称:CsvHelper,代码行数:18,代码来源:TryGetTests.cs

示例4: TryGetFieldTest

		public void TryGetFieldTest()
		{
			var data1 = new[] { "One", "Two" };
			var data2 = new[] { "1", "2" };
			var queue = new Queue<string[]>();
			queue.Enqueue( data1 );
			queue.Enqueue( data2 );
			queue.Enqueue( null );
			var parserMock = new ParserMock( queue );

			var reader = new CsvReader( parserMock );
			reader.Read();

			int field;
			var got = reader.TryGetField( 0, out field );
			Assert.IsTrue( got );
			Assert.AreEqual( 1, field );
		}
开发者ID:Core2D,项目名称:CsvHelper,代码行数:18,代码来源:TryGetTests.cs

示例5: TryGetFieldEmptyDate

        public void TryGetFieldEmptyDate()
        {
            // DateTimeConverter.IsValid() doesn't work correctly
            // so we need to test and make sure that the conversion
            // fails for an emptry string for a date.
            var data = new[] { " " };
            var queue = new Queue<string[]>();
            queue.Enqueue( data );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock );
            reader.Configuration.HasHeaderRecord = false;
            reader.Read();

            DateTime field;
            var got = reader.TryGetField( 0, out field );

            Assert.IsFalse( got );
            Assert.AreEqual( DateTime.MinValue, field );
        }
开发者ID:KPK-Teamwork-Team-Bajic,项目名称:KPK-Teamwork,代码行数:21,代码来源:TryGetTests.cs

示例6: button2_Click

        private void button2_Click(object sender, EventArgs e)
        {
            string fieldName = ConfigurationManager.AppSettings["csvFieldName"];
            XElement root = new XElement("pdfFields");
            using(var sr = new System.IO.StreamReader(openFileDialog1.FileName))
            {
                var csv = new CsvReader(sr);
                csv.Configuration.Delimiter = ";";
                while (csv.Read())
                {
                    string mapValue = null;
                    
                    csv.TryGetField<string>(fieldName, out mapValue);
                    if (!string.IsNullOrEmpty(mapValue))
                    {
                        var elem = new XElement("pdfField");
                        elem.Add(new XAttribute("originalValue" , mapValue));
                        elem.Add(new XAttribute("mapValue" , mapValue));
                        root.Add(elem); 
                    }
                }
            }
            var saveres = saveFileDialog1.ShowDialog();
            if (saveres == System.Windows.Forms.DialogResult.OK)
            {
                XDocument doc = new XDocument();
                doc.Add(root);
                try
                {
                    doc.Save(saveFileDialog1.FileName);
                    label2.Text = label2.Text + Environment.NewLine +"File " + saveFileDialog1.FileName + " was successfully saved.";
                }
                catch(Exception ex)
                {
                    label2.Text = label2.Text + Environment.NewLine + ex.Message;
                }
            }

        }
开发者ID:KonstantinDavidov,项目名称:mytestRep,代码行数:39,代码来源:Form1.cs

示例7: TryGetFieldStrictTest

        public void TryGetFieldStrictTest()
        {
            var data1 = new[] { "One", "Two" };
            var data2 = new[] { "1", "2" };
            var queue = new Queue<string[]>();
            queue.Enqueue( data1 );
            queue.Enqueue( data2 );
            queue.Enqueue( null );
            var parserMock = new ParserMock( queue );

            var reader = new CsvReader( parserMock ) { Configuration = { WillThrowOnMissingField = true } };
            reader.Read();

            int field;
            var got = reader.TryGetField( "One", out field );
            Assert.IsTrue( got );
            Assert.AreEqual( 1, field );
        }
开发者ID:KPK-Teamwork-Team-Bajic,项目名称:KPK-Teamwork,代码行数:18,代码来源:TryGetTests.cs

示例8: TryGetFieldTest

        public void TryGetFieldTest()
        {
            var isHeaderRecord = true;
            var data1 = new[] { "One", "Two" };
            var data2 = new[] { "1", "2" };
            var mockFactory = new MockRepository( MockBehavior.Default );
            var parserMock = mockFactory.Create<ICsvParser>();
            parserMock.Setup( m => m.Configuration ).Returns( new CsvConfiguration() );
            parserMock.Setup( m => m.Read() ).Returns( () =>
            {
                if( isHeaderRecord )
                {
                    isHeaderRecord = false;
                    return data1;
                }
                return data2;
            } );

            var reader = new CsvReader( parserMock.Object );
            reader.Read();

            int field;
            var got = reader.TryGetField( 0, out field );
            Assert.True( got );
            Assert.Equal( 1, field );
        }
开发者ID:rdingwall,项目名称:CsvHelper,代码行数:26,代码来源:CsvReaderTests.cs

示例9: TryGetFieldEmptyDate

        public void TryGetFieldEmptyDate()
        {
            // DateTimeConverter.IsValid() doesn't work correctly
            // so we need to test and make sure that the conversion
            // fails for an emptry string for a date.
            var data = new[] { " " };

            var mockFactory = new MockRepository( MockBehavior.Default );

            var parserMock = mockFactory.Create<ICsvParser>();
            parserMock.Setup( m => m.Configuration ).Returns( new CsvConfiguration() );
            parserMock.Setup( m => m.Read() ).Returns( data );

            var reader = new CsvReader( parserMock.Object );
            reader.Read();

            DateTime field;
            var got = reader.TryGetField( 0, out field );

            Assert.False( got );
            Assert.Equal( DateTime.MinValue, field );
        }
开发者ID:rdingwall,项目名称:CsvHelper,代码行数:22,代码来源:CsvReaderTests.cs

示例10: ReadCSVFile

        private static string[,] ReadCSVFile(string inputFile)
        {
            try
            {
                IList<string> errors = new List<string>();
                IList<string> validTimeRecord = new List<string>();
                IList<string> validValueRecord = new List<string>();
                bool hasHeaders = HasHeaders(inputFile);
                using (TextReader reader = File.OpenText(inputFile))
                {

                    var csv = new CsvReader(reader);
                    if(hasHeaders)
                    {
                        csv.Configuration.HasHeaderRecord = true;
                    }
                    while (csv.Read())
                    {
                        try
                        {

                            var time = csv.GetField<DateTime>(0);
                            bool validRow = false;
                            DateTime timeField;
                            if (csv.TryGetField(0, out timeField))
                            {
                                double valueField;
                                if (csv.TryGetField(1, out valueField))
                                {
                                    validRow = true;
                                    validTimeRecord.Add(timeField.ToString("s"));
                                    validValueRecord.Add(valueField.ToString());
                                }

                            }

                            if(!validRow)
                            {
                                errors.Add(csv.Row.ToString());
                            }

                        }catch(Exception yx)
                        {
                            Log.ErrorFormat("Error parsing CSV field. Omitting field. {0}", yx);
                        }
                    }//while

                   if(errors.Count > 0)
                    {
                        File.WriteAllLines("badrecords.log", errors.ToArray());
                    }

                    int MAX_COUNT = validTimeRecord.Count;
                    string[,] bigArr = new string[MAX_COUNT, 2];
                    for (int i = 0; i < MAX_COUNT; i++)
                    {
                        bigArr[i, 0] = validTimeRecord[i];
                        bigArr[i, 1] = validValueRecord[i];

                    }

                    return bigArr;

                }

            }catch(Exception ex)
            {
                Log.FatalFormat("Error while reading csv file {0}.", ex.Message);

                throw ex;
            }
        }
开发者ID:dynamicdeploy,项目名称:analytics-machinelearning,代码行数:72,代码来源:Program.cs

示例11: TryGetFieldStrictTest

        public void TryGetFieldStrictTest()
        {
            var isHeaderRecord = true;
            var data1 = new[] { "One", "Two" };
            var data2 = new[] { "1", "2" };
            var mockFactory = new MockFactory( MockBehavior.Default );
            var parserMock = mockFactory.Create<ICsvParser>();
            parserMock.Setup( m => m.Read() ).Returns( () =>
            {
                if( isHeaderRecord )
                {
                    isHeaderRecord = false;
                    return data1;
                }
                return data2;
            } );

            var reader = new CsvReader( parserMock.Object, new CsvReaderOptions { Strict = true } );
            reader.Read();

            int field;
            var got = reader.TryGetField( "One", out field );
            Assert.IsTrue( got );
            Assert.AreEqual( 1, field );
        }
开发者ID:heinrichbreedt,项目名称:CsvHelper,代码行数:25,代码来源:CsvReaderTests.cs

示例12: TryGetFieldInvalidTest

        public void TryGetFieldInvalidTest()
        {
            var isHeaderRecord = true;
            var data1 = new[] { "One", "Two" };
            var data2 = new[] { "1", "2" };
            var mockFactory = new MockFactory( MockBehavior.Default );
            var parserMock = mockFactory.Create<ICsvParser>();
            parserMock.Setup( m => m.Read() ).Returns( () =>
            {
                if( isHeaderRecord )
                {
                    isHeaderRecord = false;
                    return data1;
                }
                return data2;
            } );

            var reader = new CsvReader( parserMock.Object );
            reader.Read();

            string field;
            var got = reader.TryGetField( -1, out field );
            Assert.IsFalse( got );
            Assert.IsNull( field );
        }
开发者ID:heinrichbreedt,项目名称:CsvHelper,代码行数:25,代码来源:CsvReaderTests.cs

示例13: importArticles

        private bool importArticles(string filepath, Interfaces.IDatabase database)
        {
            using (System.IO.TextReader textreader = System.IO.File.OpenText(filepath))
            {
                var csv = new CsvReader(textreader);
                csv.Configuration.Delimiter = ";";

                var taskedUnits = database.ArticleUnits();
                var unitlist = taskedUnits.Result;

                var output = new List<Core.Articles.Article>();

                while (csv.Read())
                {
                    string ID;
                    if (!csv.TryGetField("Artikelnummer", out ID))
                    {
                        var task = database.GetNextArticleID();
                        ID = (task.Result).ToString();
                    }

                    string description;
                    if (!csv.TryGetField("Bezeichnung", out description))
                    {
                        description = "" ;
                    }

                    string text;
                    if (!csv.TryGetField("Text", out text))
                    {
                        text = "";
                    }

                    string unit;
                    if (!csv.TryGetField("Einheit", out unit))
                    {
                        unit = "";
                    }

                    string price1;
                    if (!csv.TryGetField("Preis 1", out price1))
                    {
                        price1 = "";
                    }

                    string tax;
                    if (!csv.TryGetField("Steuersatz", out tax))
                    {
                        tax = "";
                    }

                    var article = new Articles.Article();
                    article.ArticleID = ID;
                    article.ArticleDescription = description;
                    article.ArticleText = text;
                    article.Price1.Price1.AmountString = price1;

                    var list = from units in unitlist where units.ShortName.ToLower().Contains(unit.ToLower()) select units;
                    if (list.Count() > 0)
                        article.ArticleUnit = list.First();

                    //var saveresult = database.SaveOrUpdateArticle(article);
                    //if (!saveresult.Result)
                    //    database.SaveOrUpdateArticle(article);

                    output.Add(article);
                }
                database.SaveOrUpdateArticle(output);
            }
            return true;
        }
开发者ID:BillerFaktura,项目名称:Biller.Core,代码行数:71,代码来源:csv.cs


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