本文整理汇总了C#中CsvParser类的典型用法代码示例。如果您正苦于以下问题:C# CsvParser类的具体用法?C# CsvParser怎么用?C# CsvParser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CsvParser类属于命名空间,在下文中一共展示了CsvParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AllFieldsEmptyTest
public void AllFieldsEmptyTest()
{
using( var stream = new MemoryStream() )
using( var reader = new StreamReader( stream ) )
using( var writer = new StreamWriter( stream ) )
using( var parser = new CsvParser( reader ) )
{
writer.WriteLine( ";;;;" );
writer.WriteLine( ";;;;" );
writer.Flush();
stream.Position = 0;
parser.Configuration.Delimiter = ";;";
parser.Configuration.HasHeaderRecord = false;
var row = parser.Read();
Assert.IsNotNull( row );
Assert.AreEqual( 3, row.Length );
Assert.AreEqual( "", row[0] );
Assert.AreEqual( "", row[1] );
Assert.AreEqual( "", row[2] );
row = parser.Read();
Assert.IsNotNull( row );
Assert.AreEqual( 3, row.Length );
Assert.AreEqual( "", row[0] );
Assert.AreEqual( "", row[1] );
Assert.AreEqual( "", row[2] );
row = parser.Read();
Assert.IsNull( row );
}
}
示例2: MultipleCharDelimiter2Test
public void MultipleCharDelimiter2Test()
{
using( var stream = new MemoryStream() )
using( var reader = new StreamReader( stream ) )
using( var writer = new StreamWriter( stream ) )
using( var parser = new CsvParser( reader ) )
{
writer.WriteLine( "1``2``3" );
writer.WriteLine( "4``5``6" );
writer.Flush();
stream.Position = 0;
parser.Configuration.Delimiter = "``";
parser.Configuration.HasHeaderRecord = false;
var row = parser.Read();
Assert.IsNotNull( row );
Assert.AreEqual( 3, row.Length );
Assert.AreEqual( "1", row[0] );
Assert.AreEqual( "2", row[1] );
Assert.AreEqual( "3", row[2] );
row = parser.Read();
Assert.IsNotNull( row );
Assert.AreEqual( 3, row.Length );
Assert.AreEqual( "4", row[0] );
Assert.AreEqual( "5", row[1] );
Assert.AreEqual( "6", row[2] );
row = parser.Read();
Assert.IsNull( row );
}
}
示例3: ByteCountUsingCharWithMoreThanSingleByteTest
public void ByteCountUsingCharWithMoreThanSingleByteTest()
{
var encoding = Encoding.Unicode;
using( var stream = new MemoryStream() )
using( var writer = new StreamWriter( stream, encoding ) )
using( var reader = new StreamReader( stream, encoding ) )
using( var parser = new CsvParser( reader ) )
{
//崔钟铉
parser.Configuration.CountBytes = true;
parser.Configuration.Encoding = encoding;
writer.Write( "1,崔\r\n" );
writer.Write( "3,钟\r\n" );
writer.Write( "5,铉\r\n" );
writer.Flush();
stream.Position = 0;
parser.Read();
Assert.Equal( 8, parser.BytePosition );
parser.Read();
Assert.Equal( 18, parser.BytePosition );
parser.Read();
Assert.Equal( 28, parser.BytePosition );
parser.Read();
Assert.Equal( 30, parser.BytePosition );
}
}
示例4: Setup
public void Setup()
{
var parser = new CsvParser();
_singleLineParser = parser;
_parser = parser;
}
示例5: DifferentDelimiterTest
public void DifferentDelimiterTest()
{
using( var stream = new MemoryStream() )
using( var reader = new StreamReader( stream ) )
using( var writer = new StreamWriter( stream ) )
using( var parser = new CsvParser( reader ) )
{
writer.WriteLine( "1\t2\t3" );
writer.WriteLine( "4\t5\t6" );
writer.Flush();
stream.Position = 0;
parser.Configuration.Delimiter = "\t";
parser.Configuration.HasHeaderRecord = false;
var row = parser.Read();
Assert.IsNotNull( row );
Assert.AreEqual( 3, row.Length );
Assert.AreEqual( "1", row[0] );
Assert.AreEqual( "2", row[1] );
Assert.AreEqual( "3", row[2] );
row = parser.Read();
Assert.IsNotNull( row );
Assert.AreEqual( 3, row.Length );
Assert.AreEqual( "4", row[0] );
Assert.AreEqual( "5", row[1] );
Assert.AreEqual( "6", row[2] );
row = parser.Read();
Assert.IsNull( row );
}
}
示例6: CallbackTest
public void CallbackTest()
{
using( var stream = new MemoryStream() )
using( var reader = new StreamReader( stream ) )
using( var writer = new StreamWriter( stream ) )
using( var parser = new CsvParser( reader ) )
{
writer.WriteLine( " a\"bc\",d" );
writer.WriteLine( "\"a\"\"b\"c \" ,d" );
writer.WriteLine( "\"a\"\"b\",c" );
writer.Flush();
stream.Position = 0;
string field = null;
parser.Configuration.BadDataCallback = f => field = f;
parser.Read();
Assert.IsNotNull( field );
Assert.AreEqual( " a\"bc\"", field );
field = null;
parser.Read();
Assert.IsNotNull( field );
Assert.AreEqual( "a\"bc \" ", field );
field = null;
parser.Read();
Assert.IsNull( field );
}
}
示例7: ByteCountTestWithQuotedFieldsClosingQuoteAtStartOfBuffer
public void ByteCountTestWithQuotedFieldsClosingQuoteAtStartOfBuffer()
{
var config = new Configuration.CsvConfiguration()
{
CountBytes = true,
BufferSize = 4
};
using( var stream = new MemoryStream() )
using( var writer = new StreamWriter( stream ) )
using( var reader = new StreamReader( stream ) )
using( var parser = new CsvParser( reader, config ) )
{
writer.Write( "1,\"2\",3\r\n" );
writer.Write( "\"4\",5,\"6\"\r\n" );
writer.Flush();
stream.Position = 0;
parser.Read();
Assert.AreEqual( 9, parser.BytePosition );
parser.Read();
Assert.AreEqual( 20, parser.BytePosition );
Assert.IsNull( parser.Read() );
}
}
示例8: CharReadTotalTest
public void CharReadTotalTest()
{
using( var stream = new MemoryStream() )
using( var writer = new StreamWriter( stream ) )
using( var reader = new StreamReader( stream ) )
using( var parser = new CsvParser( reader ) )
{
parser.Configuration.AllowComments = true;
// This is a breakdown of the char counts.
// Read() will read up to the first line end char
// and any more on the line will get read with the next read.
// [I][d][,][N][a][m][e][\r][\n]
// 1 2 3 4 5 6 7 8 9
// [1][,][o][n][e][\r][\n]
// 10 11 12 13 14 15 16
// [,][\r][\n]
// 17 18 19
// [\r][\n]
// 20 21
// [#][ ][c][o][m][m][e][n][t][s][\r][\n]
// 22 23 24 25 26 27 28 29 30 31 32 33
// [2][,][t][w][o][\r][\n]
// 34 35 36 37 38 39 40
// [3][,]["][t][h][r][e][e][,][ ][f][o][u][r]["][\r][\n]
// 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
writer.WriteLine( "Id,Name" );
writer.WriteLine( "1,one" );
writer.WriteLine( "," );
writer.WriteLine( "" );
writer.WriteLine( "# comments" );
writer.WriteLine( "2,two" );
writer.WriteLine( "3,\"three, four\"" );
writer.Flush();
stream.Position = 0;
parser.Read();
Assert.Equal( 8, parser.Position );
parser.Read();
Assert.Equal( 15, parser.Position );
parser.Read();
Assert.Equal( 18, parser.Position );
parser.Read();
Assert.Equal( 39, parser.Position );
parser.Read();
Assert.Equal( 56, parser.Position );
parser.Read();
Assert.Equal( 57, parser.Position );
}
}
示例9: Parse
public static string[][] Parse(string input)
{
CsvParser parser = new CsvParser();
using (StringReader reader = new StringReader(input))
{
return parser.Parse(reader);
}
}
示例10: Test
public void Test()
{
var data = CsvReaderSampleData.SampleData1;
var reader = new StringReader(data);
var parser = new CsvParser(reader, new CsvLayout(), new CsvBehaviour());
foreach (var line in parser)
{
}
}
示例11: EnsureInternalsAreSetupWhenPassingReaderAndConfigTest
public void EnsureInternalsAreSetupWhenPassingReaderAndConfigTest()
{
using( var stream = new MemoryStream() )
using( var reader = new StreamReader( stream ) )
{
var config = new CsvConfiguration();
using( var parser = new CsvParser( reader, config ) )
{
Assert.Same( config, parser.Configuration );
}
}
}
示例12: GetCkecklist
public IEnumerable<CheckListDto> GetCkecklist()
{
Assembly assembly = Assembly.GetExecutingAssembly();
string resourceName = string.Concat(assembly.GetName().Name, '.', FILE_NAME);
Stream stream = assembly.GetManifestResourceStream(resourceName);
CsvParser<CheckListDto> parser = new CsvParser<CheckListDto>(stream);
this.list = parser.Parse().ToList();
return list;
}
示例13: EnsureInternalsAreSetupCorrectlyWhenPassingParserTest
public void EnsureInternalsAreSetupCorrectlyWhenPassingParserTest()
{
using( var stream = new MemoryStream() )
using( var reader = new StreamReader( stream ) )
{
var parser = new CsvParser( reader );
using( var csv = new CsvReader( parser ) )
{
Assert.Same( csv.Configuration, csv.Parser.Configuration );
Assert.Same( parser, csv.Parser );
}
}
}
示例14: TestNonDefaultRowDelimiterReading
public void TestNonDefaultRowDelimiterReading()
{
var settings = new CsvSettings() { RowDelimiter = "|" }; // also note, that it is single-char, not double char like default.
const String csv = "1,2,3|4,\"| as data\",\n\r as data|7";
using (var parser = new CsvParser(csv, settings))
{
String[][] rows = parser.ReadToEnd();
Assert.AreEqual(3, rows.Length, "Incorrect number of rows");
CollectionAssert.AreEqual(rows[0], new string[] { "1", "2", "3" });
CollectionAssert.AreEqual(rows[1], new string[] { "4", "| as data", "\n\r as data" });
CollectionAssert.AreEqual(rows[2], new string[] { "7" });
}
}
示例15: Main
static void Main(string[] args)
{
CsvParser parser = new CsvParser();
parser.Go();
for (int i = 0; i < 5000000; i++)
GradDown(parser.Games);
PrintAverageError(parser);
foreach (Player p in parser.Players.Where(x => x.Quality != 0).OrderByDescending(x => x.Quality))
Console.WriteLine("{0, 11} {1, 5}", p.Name, p.Quality.ToString("0.00"));
//Console.WriteLine("{0},{1}", p.Name, p.Quality.ToString("0.00"));
Console.ReadLine();
}