public void ReadAsObservable()
        {
            Cancel();

            var reader = new XsvReader(new System.IO.StreamReader(@".\ModelShips.txt"));
            var header = reader.ReadXsvLine(new[] { "," }).ToList();

            disposable = reader.ReadXsvAsObservable(new[] { "," })
                         .Where(row => !row.First().StartsWith(";"))
                         .Select(row => ModelShip.FromCsvRecord(header, row))
                         //.Where(row => row.Price >= 2500)
                         //.Where(row => row.ClassOfShip == "戦艦")
                         .ObserveOn(System.Threading.SynchronizationContext.Current)
                         .Subscribe(
                row =>
            {
                System.Diagnostics.Debug.WriteLine("OnNext");
                this.Rows.Add(row);
            },
                error =>
            {
                System.Diagnostics.Debug.WriteLine("OnError:" + error);
                reader.Dispose();
            },
                () =>
            {
                System.Diagnostics.Debug.WriteLine("OnCompleted");
                reader.Dispose();
            });
        }
Example #2
0
        public void ReadXsvObservableTest()
        {
            var csv = new List<IList<string>>();

            using (var reader = new XsvReader(new StringReader(testData01)))
            {
                csv.Add(reader.ReadXsvLine(new[] { "," }).ToArray());
#if net40
                IDisposable disposable = reader.ReadXsvAsObservable(new[] { "," }, System.Reactive.Concurrency.ThreadPoolScheduler.Instance).Subscribe(row =>
#endif
#if net45
                IDisposable disposable = reader.ReadXsvAsObservable(new[] { "," }).Subscribe(row =>
#endif
                {
                    csv.Add(row);
                    Console.WriteLine("OnNext");
                    Console.WriteLine(row.ConcatWith(", "));
                },
                e => Console.WriteLine("OnError " + e.Message),
                () =>
                {
                    Console.WriteLine("OnCompleted.");
                    foreach (var row in csv.Zip(expected01, (l, r) => new { expected = r, actual = l }))
                    {
                        Console.WriteLine(row.actual.ConcatWith("|") + "\\n");
                        CollectionAssert.AreEqual((ICollection)row.expected, (ICollection)row.actual);
                    }
                });
            }
        }
Example #3
0
        static IDisposable ReadXsvObservable(XsvReader reader)
        {
            var csv = new List <IList <string> >();

            var header = reader.ReadXsvLine(new[] { "," }).ToArray();

            csv.Add(header);

            return(reader.ReadXsvAsObservable(new[] { "," })
                   .Subscribe(row =>
            {
                csv.Add(row);
                Console.WriteLine("OnNext:" + System.Threading.Thread.CurrentThread.ManagedThreadId + " => " + row.ConcatWith(", "));
            },
                              e => Console.WriteLine("OnError " + e.Message),
                              () =>
            {
                Console.WriteLine("OnCompleted:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("");
                foreach (var row in csv)
                {
                    Console.WriteLine(row.Select(s => s.MakeXsvField(new[] { "," })).ConcatWith(", "));
                }
            }));
        }
Example #4
0
 public void ReadXsvLineTest02()
 {
     var result = new List<IList<string>>();
     using (var reader = new XsvReader(new StringReader(testData02)))
     {
         while (!reader.EndOfData)
         {
             var row = reader.ReadXsvLine(new[] { " ", "\t" }).ToArray();
             result.Add(row);
         }
     }
     foreach (var row in result.Zip(expected02, (l, r) => new { expected = r, actual = l }))
     {
         Console.WriteLine(row.actual.ConcatWith("|") + "\\n");
         CollectionAssert.AreEqual((ICollection)row.expected, (ICollection)row.actual);
     }
 }
Example #5
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Press any key.");
                Console.WriteLine("[r]- ReadXsvAsync");
                Console.WriteLine("[o]- ReadXsvObservable");
                Console.WriteLine("[a]- TextFieldParser");
                Console.WriteLine("[p]- Perfomance");
                Console.WriteLine("[q]- Perfomance2");
                Console.WriteLine("[e]- Exit");
                var key = Console.ReadKey(false);
                switch (key.KeyChar)
                {
                case 'o':
                    using (var reader = new XsvReader(new StreamReader(@".\ModelShips.txt")))
                    //using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips)))
                    {
                        var id = System.Threading.Thread.CurrentThread.ManagedThreadId;
                        Console.WriteLine("start:" + id);
                        var disposable = ReadXsvObservable(reader);
                        Console.ReadKey(false);
                        Console.WriteLine("current:" + id);
                        disposable.Dispose();
                        Console.WriteLine("Disposed");
                    }
                    break;

                case 'r':
                    //using (var reader = new XsvReader(new StreamReader(@".\ModelShips.txt")))
                    using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips)))
                    {
                        ReadXsvAsync(reader).Wait();
                    }
                    break;

                case 'a':
                    var data = "No., Name, Price, Comment" + Environment.NewLine
                               + "001, りんご, 98円, 青森産" + Environment.NewLine
                               + "002, バナナ, 120円, \"    とっても!\r\nお,い,し,い,よ!\"" + Environment.NewLine
                               + "" + Environment.NewLine
                               + "004, \"うまい棒\"\"めんたい\"\"\", 10円," + Environment.NewLine
                               + "005, バナメイ海老, 800円, \"300g\r\n\r\nエビチリに\"";
                    Console.WriteLine("<< Source >>");
                    Console.WriteLine(data);
                    Console.WriteLine("");
                    Console.WriteLine("<< XsvReader.Parse() >>");
                    foreach (var row in Parse(data))
                    {
                        Console.WriteLine(row.ConcatWith(";") + "<n>");
                    }
                    Console.WriteLine("");
                    Console.WriteLine("<< TextFieldParser (TrimWhiteSpace=true) >>");
                    foreach (var row in ReadFields(data, trimWS:true))
                    {
                        Console.WriteLine(row.ConcatWith(";") + "<n>");
                    }
                    Console.WriteLine("");
                    Console.WriteLine("<< TextFieldParser (TrimWhiteSpace=false) >>");
                    foreach (var row in ReadFields(data, trimWS:false))
                    {
                        Console.WriteLine(row.ConcatWith(";") + "<n>");
                    }
                    break;

                case 'p':
                    Console.WriteLine("");
                    var delimiters = new[] { "," };
                    var sw         = System.Diagnostics.Stopwatch.StartNew();
                    for (int i = 0; i < 1000; i++)
                    {
                        using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(Properties.Resources.ModelShips)))
                        {
                            parser.Delimiters     = delimiters;
                            parser.TrimWhiteSpace = true;
                            while (!parser.EndOfData)
                            {
                                parser.ReadFields();
                            }
                        }
                    }
                    sw.Stop();
                    Console.WriteLine(string.Format("TextFieldParser: {0} ms", sw.ElapsedMilliseconds));
                    sw.Reset();
                    sw.Start();
                    for (int i = 0; i < 1000; i++)
                    {
                        using (var reader = new XsvReader(new StringReader(Properties.Resources.ModelShips)))
                        {
                            while (!reader.EndOfData)
                            {
                                reader.ReadXsvLine(delimiters).ToArray();
                            }
                        }
                    }
                    sw.Stop();
                    Console.WriteLine(string.Format("XsvReader: {0} ms", sw.ElapsedMilliseconds));

                    break;

#if net45
                case 'q':
                    ReadAsyncTest().Wait();
                    break;
#endif
                case 'e':
                    return;
                }
                Console.WriteLine("***-***-***-***");
            }
        }