如何将Dictionary(集合)对象写入文件(VB .Net 2010)

时间:2012-05-04 23:35:32

标签: vb.net collections dictionary vb.net-2010

我正在尝试将字典集合写入文件。

Collection的结构如下:

GlobalCollection    (Collection of TestCollection)
    (0)
    [KEY]
    [Value]
    - TotalGlobal_W_Count    (Integer)
    - TotalGlobal_I_Count    (Integer)
    - TotalGlobal_F_Count    (Integer)
    TestCollection    (Dictionary - Key, Value)
        (0)
        [KEY]
        [Value]
        - NumberOfTests        (Integer)
        - TestCollectionName    (String)
        - TypesOfTests        (ArrayList)
        ResultsCollection    (Dictionary - Key, Value)
            (0)
            [KEY]
            [Value]
            - TotalFcount    (Integer)
            - TotalIcount    (Integer)
            - TotalWcount    (Integer)
            - ID        (String)
            - TestFolderType(String)
            - TestPassed    (Boolean)
            - FullLog    (Array)
            ResultsCollection    (Dictionary - Key, Value)
                (0)
                [KEY]
                [Value]
                - LineNumber    (Integer)
                - MessageType    (String)
                - ResultString    (String)

我想将所有上述变量写入文本文件,同时保持层次结构格式。 (注意每个集合对象中可以有多个元素)

感谢!!!

2 个答案:

答案 0 :(得分:4)

将其序列化为Xml。 Here is a StackOverflow question that answers the topic。一个特别好的读物是@John Saunders在评论中的链接。

答案 1 :(得分:4)

比xml更快更小,如果你不打算解析/ ...序列化文件,就是使用BinaryFormater:

Dim MyFormatter As New BinaryFormatter()
Dim MyFile As New FileStream("Serialized.ser", FileMode.Create, FileAccess.Write, FileShare.None)
MyFormatter.Serialize(MyFile, TheObjectIWantToSerialize)
MyFile.Close()