Skip to content

QuickManifoldWrite

Cinchoo edited this page Dec 11, 2017 · 2 revisions

Writing Manifold (Multi record) file

To create multi record file, use ChoManifoldWriter component to write it. Sample below shows how to create one.

First define POCO classes with specifications. Customer class is defined to create the records in CSV format, Order class is defined to create the records in fixed length format.

    [ChoCSVRecordObject(";")]
    public class Customer
    {
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }

    [ChoFixedLengthRecordObject]
    public class Order
    {
        [ChoFixedLengthRecordField(0, 8)]
        [ChoDefaultValue("() => DateTime.Now")]
        public string OrderDate { get; set; }

        [ChoFixedLengthRecordField(8, 3)]
        public int Id { get; set; }

        [ChoFixedLengthRecordField(11, 3)]
        public string Code { get; set; }
    }

Write list of objects

List<object> objs = new List<object>();
Customr s = new Customer() { CustomerID = "Raj", CompanyName = "ABC Inc" };
objs.Add(s);

var o = new Order { Id = 100, Code = "ABC" };
objs.Add(o);

using (var stream = new MemoryStream())
using (var reader = new StreamReader(stream))
using (var writer = new StreamWriter(stream))
using (var parser = new ChoManifoldWriter(writer))
{
    parser.Write(objs);

    writer.Flush();
    stream.Position = 0;

    Console.WriteLine(reader.ReadToEnd());
}

Write each object

using (var parser = new ChoCSVWriter("Emp.csv"))
{
    Customr s = new Customer() { CustomerID = "Raj", CompanyName = "ABC Inc" };
    parser.Write(s);

    var o = new Order { Id = 100, Code = "ABC" };
    parser.Write(o);
}