【问题标题】:How to write a Dictionary (collection) object to file (VB .Net 2010)如何将字典(集合)对象写入文件(VB .Net 2010)
【发布时间】:2024-04-28 11:15:02
【问题描述】:

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

集合的结构如下:

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)

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

谢谢!!!

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    比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()
    

    【讨论】:

    • 感谢以上内容,遗憾的是我们需要格式化...但是很好的答案。
    • 是的,确实,GameAlchemist,二进制非常快。 (我为很多工作选择了二进制,因为 xml 是一个选项,我了解到如果你需要速度二进制会更快)
    最近更新 更多