Giter VIP home page Giter VIP logo

csvserializer's Introduction

CsvSerializer

Convert an object graph to CSV.

Basic Usage

var target = new[]
{
	new Person { Name = "Nate Zaugg", Department = "Management", Charisma = 100, Address = new Address { Street1 = "123 Fake Street", Address2 = "C/O Homer", City = "Springfield" } },
	new Person { Name = "Dan Beus", Department = "IT", Charisma = 110, Address = new Address { Street1 = "872 Heratigte Park Blvd", Address2 = "Suite 200", City = "Layton" } },
	new Person { Name = "Phil Gilmore", Department = "AV", Charisma = 110, Address = new Address { Street1 = "53 Broad Street", Address2 = "#600", City = "Salt Lake City" } },
} 

using (var fs = File.Open(@"C:\Temp\Output.csv"))
{
	ISerializer serializer = new CsvSerializer(); // Obviously do this via DI
	serializer.Serialize(fs, target);
}

// The file output would be:
Name,Department,Charisma,Address.Street1,Address.Street2,Address.City
"Nate Zaugg",Management,100,"123 Fake Street","C/O Homer",Springfield
"Dan Beus",IT,110,"872 Heratigte Park Blvd","Suite 200",Layton
"Phil Gilmore",AV,110,"53 Broad Street",#600,"Salt Lake City"

Settings

The settings can be modified to change the output. The default settings are fully compatible with Microsoft Excel, but there may be times where you want something else to be output.

serializer.Settings.WriteHeaders = false;

WriteHeaders

Specifies if a header should be generated. Default: true (bool)

QuoteAllValues

Specifies if all fields should be quoted, if it's required or not. Default: false (bool)

FieldDelimeter

The delimeter between each field in a row. Default: "," (string)

QuoteDelimeter

The delimeter used to quote fields in a row. Default: """ (string)

NewLineDelimeter

The new line string used at the end of a row. Default: "CRLF" (string)

RemoveLineBreaksInFields

Specifies if CR or LF values should be stripped from field output and replaced with whitespace. Default: True (bool)

TextEncoding

Specifies the text encoder that should be used to write values. Default: System.Text.UTF8 (System.Text.Encoding)

ShowFullNamePath

Specifies if sub-classes should show their parents type as part of their name. E.g. "Person.Address.Line1". Default: true (bool)

NamePathDelimeter

The delimeter between each part of a path. E.g. "Person.Address.Line1". Default: "." (string)

UseXmlAttributes

Specifies if attribute tags like [XmlIgnore] or [XmlElement(Name="value")] should be observed. Default: true (bool)

UseJsonAttributes

Specifies if attribute tags like [JsonIgnore] or [JsonProperty("value")] or [JsonRequired] should be observed. Default: true (bool)

UseSerializerAttributes

Specifies if attribute tags like [NonSerialized] or [DataMember(Name="value")] should be observed. Default: true (bool)

UseDataAnnotationAttributes

Specifies if attribute tags like [StringLength] should be observed. Default: true (bool)

ConvertChildCollectionsToRows

Indicates if a collection is detected as a property on the object to be serialized, the collection will be converted to rows. E.g. If there is a Person with 3 addresses, there will be columns for Person.Address1.City, Person.Address2.City, and Person.Address3.City. Alternativly, the rows in a child collection will cause the parent column values to be repeated or omitted based on the value of the FlattenHeirarchicalStructuresWithEmptyRows property. Default: true (bool)

FlattenHeirarchicalStructuresWithEmptyRows

Indicates if a heirarchy of objects should use the parental row values for each of the child rows or if the child rows are surrounded by empty values. Default: true (bool)

Note: This property is only active if ConvertChildCollectionsToRows is set to true.

csvserializer's People

Contributors

nzaugg avatar natezaugg avatar zthurston avatar mephisztoe avatar mdmeyer avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.