Is there such thing as a CSV Serializer? (similar to XmlSerializer)

前端 未结 3 881
-上瘾入骨i
-上瘾入骨i 2021-01-17 10:18

I am toying around with serializing and deserializing CSV files and I am wondering if there is an existing library, similar in concept to the XmlSerializer, which can declar

3条回答
  •  甜味超标
    2021-01-17 10:41

    You should take a look into FileHelpers Library.

    Some sample code from their site:

    using FileHelpers; 
    
    // First declare the record class 
    [DelimitedRecord(",")] 
    public class SampleType 
    { 
        public string Field1; 
        public int    Field2; 
    } 
    
    public void WriteExample() 
    { 
        FileHelperEngine engine = new FileHelperEngine(typeof(SampleType)); 
    
        SampleType[] records = new SampleType[1]; 
    
        records[0] = new SampleType(); 
        records[0].Field1 = "Hello World"; 
        records[0].Field2 = 12; 
    
        engine.WriteFile("destination.txt", records); 
    
        // Now the file contains the created record in this format: 
        //  
        // Hello World,12 
    } 
    

提交回复
热议问题