windows phone 7 - loading a big xml file

2019-04-09 10:46发布

问题:

I am trying to load a xml file to my app and show it on a ListBox. The problem is that the xml file is about 5 MB, and it takes about 40 seconds on the phone to load. and when the phone locks the screen and returns back it takes another 40 seconds. I tried to use the isolatedstorage to store the data on it but it didn't improve performance. This is my code to load the data:

XDocument loadedData = XDocument.Load("BigFile.xml");
            var data = from query in loadedData.Descendants("w")
                       orderby (string)query.Element("e")
                       select new myClass
                       {
                           First = (string)query.Element("a"),
                           Second = (string)query.Element("e")
                       };
        ListBox.ItemsSource = data.ToList<myClass>();

Is there anyway to speed up the loading?

回答1:

To improve performance you can do the following:

  • Make sure the loading is moved off the ui thread
  • Break the file into multiple smaller ones and load them one at a time and updating the list as you add each one.
  • If possible, reformat the data (before adding to the app) so it only includes the absolute minimum data used by the app.
  • Convert the file to a different format (try JSON) which can be parsed faster.
  • (If using 7.1) put the data in a database and deploy that in the XAP.
  • Reconsider the need to have so much data. Large/long lists are hard to browse on a device with a small screen.


回答2:

You could try putting that code into a background thread to do its stuff while you can get on with other stuff in your main thread.

delcare "private BackgroundWorker backgroundWorker;" in the MainPage partial class

Put this code in your MainPage() constuctor (or MainPage_loaded)

// Initiaze background worker do display favourites

        if (backgroundWorker == null)
        {
            backgroundWorker = new BackgroundWorker();
            backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
        }
        backgroundWorker.RunWorkerAsync();

and then have your code in here:

    void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
       XDocument loadedData = XDocument.Load("BigFile.xml");
        var data = from query in loadedData.Descendants("w")
                   orderby (string)query.Element("e")
                   select new myClass
                   {
                       First = (string)query.Element("a"),
                       Second = (string)query.Element("e")
                   };
    ListBox.ItemsSource = data.ToList<myClass>();

    }

Hopefully this should do the trick.