views:

44

answers:

3

I have a list of custom types, they need to be saved/loaded to/from a file on a mobile device (Windows Mobile 6) what method would be the most suited, taking into account the limited resources of the device? EDIT: The data file will be around 2-5mb

+1  A: 

Devices capable of running Win Mobile 6, seem to be able to handle themselves quite well using the "crippled" .NET framework.

But if you want something faster then what System.IO provides, I think you're out of luck (?).

With "custom types", I gather you're referring to classes, and by that I suspect some .NET compliant classes? Then I don't see how you're going to squeeze any more juice...at least no more than
this happy camper got.

Morten Bergfall
+2  A: 

How much data are we talking about?

I used an old device (HTC-s620, with a TI OMAP 850 200 MHz processor) to save a 2mb XML file using XML Serialization, in a matter of 3-5 seconds. Very simple programming model. Very easy to do. With a newer device I'm sure it would be much faster.

My usage scenario was, one full load, and one full save, per run.

[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;
    }

}
Cheeso
Main issue with XML is that you can't perform "naive" writes. You need backups (copy, flush, delete, move, or just copy, flush and delete the copy) cause in a 2-3 second window the power can die and corrupt your file.
Quibblesome
+1  A: 

I'd recommend looking at SqlLite if you want a proper db but without the bloat. SqlLite is also atomic and power resilient. Otherwise saving to a flat file isn't a problem. Just remember that the power can die on you at any point, including half-way through your write.

Quibblesome