2009-12-21 66 views

回答

2

我們在談論多少數據?

我使用舊設備(HTC-s620,TI OMAP 850 200 MHz處理器)在3-5秒內使用XML序列化保存2mb XML文件。非常簡單的編程模型。很容易做到。有了更新的設備,我相信它會更快。

我的使用場景是每次運行一次滿載和一次完整保存。

[XmlRoot("notes")] 
public class NoteList : List<Note> 
{ 
    // Set this to 'default' or 'preserve'. 
    [XmlAttribute("space", Namespace = "http://www.w3.org/XML/1998/namespace")] 
    public string space = "preserve"; 

    public static void Save(NoteList noteList, string NotesFilePath) 
    { 
     if (noteList == null) return; 
     XmlSerializer serializer = new XmlSerializer(typeof(NoteList)); 

     string tmpPath = NotesFilePath + ".tmp"; 
     using (System.IO.FileStream fs = new FileStream(tmpPath, FileMode.Create, FileAccess.Write)) 
     { 
      serializer.Serialize(fs, noteList); 
      fs.Close(); 
     } 

     if (File.Exists(tmpPath)) 
     { 
      if (File.Exists(NotesFilePath)) 
      { 
       string oldFile = NotesFilePath + ".bak"; 
       if (File.Exists(oldFile)) File.Delete(oldFile); 
       File.Move(NotesFilePath, oldFile); 
      } 
      File.Move(tmpPath, NotesFilePath); 
     } 
    } 


    public static NoteList Load(string NotesFilePath) 
    { 
     if (!System.IO.File.Exists(NotesFilePath)) 
      return null; 

     NoteList noteList = new NoteList(); 
     XmlSerializer serializer = new XmlSerializer(noteList.GetType()); 
     using (FileStream fs = new FileStream(NotesFilePath, FileMode.Open, FileAccess.Read)) 
     { 
      noteList = (NoteList)serializer.Deserialize(fs); 
      fs.Close(); 
     } 

     return noteList; 
    } 

} 
+0

XML的主要問題是您無法執行「天真」寫入。您需要備份(複製,刷新,刪除,移動或僅複製,刷新和刪除副本),因爲在2-3秒的時間內,電源會死機並損壞您的文件。 – Quibblesome 2009-12-21 11:45:10

1

能夠運行Win Mobile 6的設備似乎能夠使用「殘缺」的.NET框架很好地處理自己。但是如果你想要更快的東西,那麼System.IO提供了什麼,我認爲你運氣不好(?)。

「自定義類型」,我收集你指的類,並由此我懷疑一些.NET兼容的類?然後,我不知道你將如何擠出更多的果汁...至少不會超過
this happy camper了。

1

我建議看看SqlLite,如果你想要一個適當的分貝,但沒有膨脹。 SqlLite也具有原子性和電源彈性。否則,保存到平面文件不是問題。請記住,在任何時候,權力都會在你身上消失,包括你寫作的一半。

相關問題