0

I am building a Windows Phone 8.1 RT App which runs MP3 files in Background task.

I followed the steps in the sample code shown here : http://code.msdn.microsoft.com/windowsapps/BackgroundAudio-63bbc319

In MyPlaylistManager Project I would like to build my own Playlist from XML file.

When I try to access this XML file I am getting an exception.

An exception of type 'System.Xml.XmlException' occurred in SYSTEM.XML.NI.DLL but was not handled in user code

Additional information: An internal error has occurred.

I have added the XML file to the Project and set

Build Action : Content

Copy to Output Directory : Copy if newer

To access the file I tired below options :

  XDocument xdoc = XDocument.Load("ms-appx:///XYZ.xml");

  XDocument xdoc = XDocument.Load("XYZ.xml");

XDocument.Load is supported in Windows Phone 8.1 as per the documentation: http://msdn.microsoft.com/en-us/library/bb343181(v=vs.110).aspx

Directory Structure of my Solution

Any pointers on what I am doing wrong. Thanks.

Community
  • 1
  • 1
Saqib Vaid
  • 412
  • 6
  • 23
  • Remember that I am trying to do this in a background task – Saqib Vaid Aug 13 '14 at 06:07
  • 1
    DOes it work when you deserialize it in the foreground process? Can you also check if the file exists and maybe its content - `StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("XYZ.xml");`? – Romasz Aug 13 '14 at 08:08
  • I tried your code but to check if file exists : An exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.ni.dll but was not handled in user code. You r right, its not able to find the file. – Saqib Vaid Aug 13 '14 at 09:32
  • @Romasz When I try GetFileAsync("ms-appx:///XYZ.xml"); I get An exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll but was not handled in user code Additional information: Value does not fall within the expected range. – Saqib Vaid Aug 13 '14 at 09:34

1 Answers1

1

I think that the problem is, because you aren't providing the correct path. Your file is in project MyPlaylistManager. Try to use:

XDocument xdoc = XDocument.Load("ms-appx:///MyPlaylistManager/Quran.xml");
// or like this:
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"MyPlaylistManager\Quran.xml");

Also not forget to include MyPlaylistManager in references. Maybe this answer will also help a little.

EDIT - you may also try to use XDocument.Load(Stream) - then in your BackgroundTask first get the StorageFile then load XDocument using Stream got from the file.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Now I get the file when I use StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"SampleBackgroundAudio.MyPlaylistManager\Quran.xml"); had to give Namespace also. – Saqib Vaid Aug 13 '14 at 11:28
  • @SaqibVaid Now, once you get the file, you should be able to process it. Of course there is a chance that there can be other problems. – Romasz Aug 13 '14 at 11:30
  • But When I try XDocument xdoc = XDocument.Load("ms-appx:///SampleBackgroundAudio.MyPlaylistManager/Quran.xml"); it throws an error "An internal error has occurred." – Saqib Vaid Aug 13 '14 at 11:31
  • Is this because XML file is corrupted or when I am using ms-appx the file path must be specified in a certain way... – Saqib Vaid Aug 13 '14 at 11:40
  • @SaqibVaid First of all do everyting in foreground process, then when it's working move it to the background task. I'm not sure what can be a problem. You may try get storagefile, then `XDocument.Load(fileStream)` - to see if that works. – Romasz Aug 14 '14 at 05:32
  • Accessing the file & XDocument.Load works fine when I do it in the foreground process. – Saqib Vaid Aug 14 '14 at 06:20
  • But in the Background process I can read the file but XDocument.Load fails. Not sure why ! – Saqib Vaid Aug 14 '14 at 06:21
  • Doing XDocument.Load from Stream Works in Background as well. StorageFile storageFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"SampleBackgroundAudio.MyPlaylistManager\Quran.xml"); var randomAccessStream = await storageFile.OpenReadAsync(); Stream stream = randomAccessStream.AsStreamForRead(); XDocument xdoc = XDocument.Load(stream); //WORKS !!! – Saqib Vaid Aug 14 '14 at 06:22
  • Can you update your answer with this so I can Mark It as Answer. – Saqib Vaid Aug 14 '14 at 06:23
  • @SaqibVaid I've updated the answer. Glad that this works, thought I'm not still sure why URI version hadn't worked. – Romasz Aug 14 '14 at 06:26