(德)从CSV序列化到一个对象(或最好的对象类型的列表) [英] (de)Serialize from CSV to an object (or preferably a list of type object)

查看:770
本文介绍了(德)从CSV序列化到一个对象(或最好的对象类型的列表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我是一个C ++程序员 - 努力学习C# - 这似乎有一些内置的对象序列化 - 但我有点出的水在这里...)

(I am a C++ programmer - trying to learn C# - and it appears that there are some built in object serialization - but I am a bit out of water here...)

我被要求测试数据加载到从CSV文件对象的集合。 (CSV优于XML,因为它的简单多了赫克和可读性于人类。我们createing测试数据运行单元测试)

I have been asked to load test data into a collection of objects from a CSV file. (CSV is preferred over xml because it is a heck of a lot simpler and readable to humans. We are createing test data to run unit tests)

该系列将是一个(简单)对象列表

The collection will be a list of (simple) objects.

List<MyObject>



在哪里myObject的是包含了一些双打和几个整数的类。

Where MyObject is a class that contains a few doubles and a few ints.

什么是做到这一点的首选方法是什么?我不认为我永远不会有必要,只是这些序列化对象之一。

What is the preferred way to do this? I don't think I will ever have a need to serialize just one of these objects.

我期望一个单一的线代表一个对象,以及一组线在该文件中是用来做列表/集合。

I would expect that one single line represents one object, and the set of lines in the file is used to make the list/collection.

推荐答案

下面应该做的伎俩,只是做这个几个月前数据转换项目我工作。

Below should do the trick, just did this a couple months ago for data conversion project i was working on.

一个重要注意事项给任何人做CSV(或任何字符分隔的字符串),你真的应该看看使用适合于特定库它。最流行的,我发现低于(相当尝试和真正的)

A major note to anyone doing CSV (or any character delimited strings), you really should look at using a specific library suited for it. The most popular one I've found is below (pretty tried and true)

您将与 string.Split开始('') ,然后你会怎么做,当有人在您的测试数据文件意外地抛出一个逗号? ......这是噩梦后只是恶梦,这个库已经走过最适合您:

You'll start with string.Split(',') and then what do you do when someone accidentally throws a comma in your test data file? ... it's just nightmare after nightmare and this library has traversed most of that for you:

http://www.codeproject.com/KB/database/CsvReader.aspx?msg=3227161

using System.IO;
using LumenWorks.Framework.IO.Csv;

static class Program
{

    public class MyObject
    {
        public int Prop1 { get; set; }
        public string Prop2 { get; set; }
        public decimal Prop3 { get; set; }
    }

    void ReadCsv()
    {
        //holds the property mappings
        Dictionary<string, int> myObjectMap = new Dictionary<string, int>();

        List<MyObject> myObjectList = new List<MyObject>();

        // open the file "data.csv" which is a CSV file with headers
        using (CsvReader csv = new CsvReader(new StreamReader("data.csv"), true))
        {
            int fieldCount = csv.FieldCount;
            string[] headers = csv.GetFieldHeaders();

            for (int i = 0; i < fieldCount; i++)
            {
                myObjectMap[headers[i]] = i; // track the index of each column name
            }

            while (csv.ReadNextRecord())
            {
                MyObject myObj = new MyObject();

                myObj.Prop1 = csv[myObjectMap["Prop1"]];
                myObj.Prop2 = csv[myObjectMap["Prop2"]];
                myObj.Prop3 = csv[myObjectMap["Prop3"]];

                myObjectList.Add(myObj);
            }
        }
    }
}  

这篇关于(德)从CSV序列化到一个对象(或最好的对象类型的列表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆