简体   繁体   中英

How to write and read list<> from text files in C#

English is not my native language and I am newbie, so don't laugh at me.

I want to create a class in C# that help me to save data to file and read them easily. It works like this:

RealObject john = new RealObject("John");
john.AddCharacter("Full Name", "Something John");
john.AddCharacter("Grade", new List<double> { 9.9, 8.8, 7.7 });
await john.SaveToFileAsync("Test.ini");

RealObject student = new RealObject("John");
await student.ReadFromFileAsync("Test.ini");
Type valueType = student.GetCharacter("Grade").Type;
List<double> johnGrade = (List<double>) student.GetCharacter("Grade").Value;

The file "Test.ini" looks like this:

S_Obj_John
Name    System.String   Something John
Grade   System.Collections.Generic.List`1[System.Double]    9.9;8.8;7.7
E_Obj_John

I have some questions:

Question 1. Can you give me some libraries that do this job for me, please?

Question 2. My code is too redundant, how can I optimize it?

2.1 Saving code: I have to write similar functions: ByteListToString, IntListToString, DoubleListToString,...

    private static string ListToString(Type listType, object listValue)
    {
        string typeString = GetBaseTypeOfList(listType);
        if (typeString == null)
        {
            return null;
        }
        switch (typeString)
        {
            case "Byte":
                return ByteListToString(listValue);
            ..........
            default:
                return null;
        }
    }
    private static string ByteListToString(object listValue)
    {
        List<byte> values = (List<byte>) listValue;
        string text = "";
        for (int i = 0; i < values.Count; i++)
        {
            if (i > 0)
            {
                text += ARRAY_SEPARATING_SYMBOL;
            }
            text += values[i].ToString();
        }
        return text;
    }

2.2 Reading code: I have to write similar functions: StringToByteList, StringToIntList, StringToDoubleList,...

    public static object StringToList(Type listType, string listValueString)
    {
        string typeString = GetBaseTypeOfList(listType);
        if (typeString == null)
        {
            return null;
        }
        switch (typeString)
        {
            case "Byte":
                return StringToByteList(listValueString);
            ..........
            default:
                return null;
        }
    }
    private static List<byte> StringToByteList(string listValueString)
    {
        var valuesString = listValueString.Split(ARRAY_SEPARATING_SYMBOL);
        List<byte> values = new List<byte>(valuesString.Length);
        foreach (var v in valuesString)
        {
            byte tmp;
            if (byte.TryParse(v, out tmp))
            {
                values.Add(tmp);
            }
        }
        return values;
    }

Thank you for your help

There are two ways two common ways to "serialize" data, which is a fancy way of taking an object and turning it into a string. Then on the other side you can "deserialize" that string and turn it back into an object. Many folks like JSON because it is really simple, XML is still used and can be useful for complex structures but for simple classes JSON is really nice.

I would check out https://www.json.org/ and explore, libraries exist that will serialize and deserialize for you which is nice. Trying to do it with string manipulation is not recommended as most people (including me) will mess it up.

The idea though is to start and end with objects, so take an object and serialize it to save it to the file. Then read that object (really just a string or line of text in the file) and deserialize it back into an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM