通过资源文件(txt)将数据保存在SortedDictionary中

时间:2014-08-28 11:42:38

标签: c# .net dictionary

我有一个txt文件保存在资源文件(* .resx)中,其中包含一些信息:

    23,TRUNK-1,Trunk-1,,[Barry_Boehm]   
    24,TRUNK-2,Trunk-2,,[Barry_Boehm]   
    25,LEAF-1,Leaf-1,,[Barry_Boehm] 
    26,LEAF-2,Leaf-2,,[Barry_Boehm] 
    136,UDPLite,,,[RFC3828] 

...并希望将第一个和第二个条目保存到SortedDictionary:

    23,TRUNK-1  
    24,TRUNK-2  
    25,LEAF-1
    26,LEAF-2
    136,UDPLite

public static SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();

String[] rows = Regex.Split(Resources.ProTypes.ProTypesSource, "\r\n");

foreach (var i in rows)
{
    String[] words = i.Split(new[] { ',' });

    ...

    xTypes.Add(proNumber, proName);
}

我该怎么做?

3 个答案:

答案 0 :(得分:1)

你可以这样做:

        SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();

        String[] rows = Regex.Split("23,TRUNK-1,Trunk-1,,[Barry_Boehm]", "\r\n");

        foreach (var i in rows)
        {
            String[] words = i.Split(new[] { ',' });

            UInt16 proNumber = Convert.ToUInt16(words[0]);
            string proName = words[1];

            xTypes.Add(proNumber, proName);
        }

答案 1 :(得分:1)

看起来你几乎做了所有事情:

foreach (var i in rows)
{
    String[] words = i.Split(new[] { ',' });

    UInt16 proNumber= UInt16.Parse(words[0]);
    string  proName=words[1];

    xTypes.Add(proNumber, proName);
}

答案 2 :(得分:1)

public static SortedDictionary<UInt16, string> xTypes = new SortedDictionary<UInt16, string>();

String[] rows = Regex.Split(Resources.ProTypes.ProTypesSource, "\r\n");

foreach (var i in rows){
    String[] words = i.Split(new[] { ',' });
    xTypes.Add(UInt16.Parse(words[0]), words[1]);
}