Ejemplo n.º 1
0
 /// <summary>
 /// Serializes to a file.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="Input">The collection to serialize.</param>
 /// <param name="Path">The output path.</param>
 /// <param name="Encoding">The output Encoding.</param>
 /// <param name="Options">The options for serialization.</param>
 /// <returns> <paramref name="Input"/></returns>
 public static IEnumerable <T> CsvSerialize <T>(this IEnumerable <T> Input
                                                , string Path
                                                , Encoding Encoding
                                                , CsvSerializationOptions Options)
 {
     using (var Writer = new StreamWriter(new FileStream(Path, FileMode.Create), Encoding))
     {
         CsvSerializer.Serialize(Writer, Input, Options);
         return(Input);
     }
 }
Ejemplo n.º 2
0
        public void TestSeparator()
        {
            var SerializationOptions = new CsvSerializationOptions
            {
                Separator = ';',
            };

            var Output = new MemoryStream();

            using (var Writer = new StreamWriter(Output, System.Text.Encoding.ASCII, 1024, true))
            {
                CsvSerializer.Serialize(Writer, PropertyBasedData, SerializationOptions);
            }
            Output.Seek(0, 0);

            var    Actual   = new StreamReader(Output).ReadToEnd();
            string Expected = "sep=;(\r\n|\n)Prop1;Prop2(\r\n|\n)Hello;World(\r\n|\n)Good to;See you";

            Assert.Matches(Expected, Actual);
        }
Ejemplo n.º 3
0
        public void TestLocalizableFields()
        {
            var SerializationOptions = new CsvSerializationOptions
            {
                Separator = ';',
                Culture   = CultureInfo.GetCultureInfo("nl-nl")
            };

            var Data = new List <HasLocalizableFields>
            {
                new HasLocalizableFields {
                    Double = 1.1, Float = 2.2F, Date = new DateTime(2018, 12, 23)
                },
            };

            var Output = new MemoryStream();

            using (var Writer = new StreamWriter(Output, System.Text.Encoding.ASCII, 1024, true))
            {
                CsvSerializer.Serialize(Writer, Data, SerializationOptions);
            }
            Output.Seek(0, 0);

            var    Actual   = new StreamReader(Output).ReadToEnd();
            string Expected = "sep=;(\r\n|\n)Double;Float;Date(\r\n|\n)1,1;2,2;23-12-2018";

            Assert.Matches(Expected, Actual);


            SerializationOptions.Culture = CultureInfo.GetCultureInfo("en-us");
            Output = new MemoryStream();
            using (var Writer = new StreamWriter(Output, System.Text.Encoding.ASCII, 1024, true))
            {
                CsvSerializer.Serialize(Writer, Data, SerializationOptions);
            }
            Output.Seek(0, 0);

            Actual   = new StreamReader(Output).ReadToEnd();
            Expected = "sep=;(\r\n|\n)Double;Float;Date(\r\n|\n)1.1;2.2;12-23-2018";
            Assert.Matches(Expected, Actual);
        }
Ejemplo n.º 4
0
        public void TestQualifierEnabled()
        {
            var SerializationOptions = new CsvSerializationOptions
            {
                UseTextQualifier = true,
            };

            var Output = new MemoryStream();

            using (var Writer = new StreamWriter(Output, System.Text.Encoding.ASCII, 1024, true))
            {
                CsvSerializer.Serialize(Writer, PropertyBasedData, SerializationOptions);
            }

            Output.Seek(0, 0);

            var    Actual   = new StreamReader(Output).ReadToEnd();
            string Expected = "Prop1,Prop2(\r\n|\n)\"Hello\",\"World\"(\r\n|\n)\"Good to\",\"See you\"";

            Assert.Matches(Expected, Actual);
        }
Ejemplo n.º 5
0
        public void TestLineNumbersEnabled()
        {
            var SerializationOptions = new CsvSerializationOptions
            {
                UseRowNumbers        = true,
                RowNumberColumnTitle = "#",
            };


            var Output = new MemoryStream();

            using (var Writer = new StreamWriter(Output, System.Text.Encoding.ASCII, 1024, true))
            {
                CsvSerializer.Serialize(Writer, PropertyBasedData, SerializationOptions);
            }
            Output.Seek(0, 0);

            var    Actual   = new StreamReader(Output).ReadToEnd();
            string Expected = "#,Prop1,Prop2(\r\n|\n)1,Hello,World(\r\n|\n)2,Good to,See you";

            Assert.Matches(Expected, Actual);
        }