2012-05-04 48 views
2

我正在嘗試將字典集合寫入文件。如何將字典(集合)對象寫入文件(VB .Net 2010)

收集的結構,像這樣:

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) 

我想寫出所有上述變量的文本文件,同時保持層次格式。 (注意每個收集對象中可以有多少個元素)

謝謝!

+1

你看着XML序列化?查看XmlSerializer類。 http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx –

回答

3

更快,比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() 
+0

感謝上述,可惜我們需要格式化...但很好的答案。 – Destroyer

+0

是的,的確,GameAlchemist,binary是非常快的。 (我選擇二進制進行了很​​多工作,因爲xml是一個選項,我知道如果你需要更快的二進制速度) – wrtsvkrfm