2

I am attempting to write a Dictionary Collection to a file.

The Collection is structured like so:

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)

I would like to write out all of the above variables to a text file while keeping the hierarchy formatting. (Note there can be number of elements in each collection object)

Thanks!!!

Destroyer
  • 639
  • 1
  • 7
  • 13
  • 1
    Have you looked into XML Serialization? Check out the XmlSerializer class. http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx – George Johnston May 04 '12 at 23:43

2 Answers2

4

faster and smaller than xml, if you don't plan to parse/... the serialized file, is to use a 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
  • 18,995
  • 7
  • 36
  • 59
  • Thanks for the above, sadly we need formatting... but great answer. – Destroyer May 08 '12 at 01:50
  • Yes, indeed, GameAlchemist, binary is very fast. (i chose binary for a lot of work, since xml was an option and I learned that if you need speed binary is faster) –  Feb 15 '15 at 15:36
3

Serialize it to Xml. Here is a StackOverflow question that answers the topic. An especially good read is the link by @John Saunders in the comments.

Community
  • 1
  • 1
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104