views:

238

answers:

3

I had a look at this previous question however this doesn't seem to work for XNA 4 for a Windows Phone 7 project: http://stackoverflow.com/questions/1234426/xna-framework-importers

I've been trying to use:

string line; 
using (StreamReader sr = new StreamReader("credits.txt"))
{
    while ((line = sr.ReadLine()) != null)
    {
         //reads line by line until eof        
         //do whatever you want with the text
    }
}

`

but this is throwing a System.MethodAccessException "Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)"

Do I need to look at using IsolatedStorage for this instead?

Edit: Please note I am trying to load a pre-prepared file, not save settings for a running application. e.g. a text file containing credits that I will only read in at run time but need to be able to edit during design time.

A: 

Do I need to look at using IsolatedStorage for this instead?

yep, every app (except OS apps) needs to use IsolatedStorage to store data in physical memory OR you can use could service to sore the data.

IsolatedStorage example

lukas
Thanks Lukas. As per the previous question, I'm trying to load up a file that I will package with the application, such as credits for the game. I'm not sure if I am able to package a file directly in to isolated storage at design time.
Sebastian Gray
+1  A: 

Found it; I can do this without IsolatedStorage, just need to use an XML file structured as such:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
  <Asset Type="System.String">
    <credit>
      Firece Game Hunting

      Developer : Sebastian Gray

      Deer (CC) : Martin Pettitt
      http://www.flickr.com/photos/mdpettitt

    </credit>
  </Asset>
</XnaContent>

and then load the XML file like this:

public string LoadFromFile()
{
    using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create("XMLFile1.xml"))
    {
        reader.MoveToContent();
        reader.ReadToFollowing("credit");
        credits = reader.ReadInnerXml();
    }
    return credits;
}

The XML file can just be added to the the normal project (not the content project) and set the build action to 'Content' and the Copy to Output Directory to 'Copy always'.

Sebastian Gray
Setting the build action to "Content" and copying to "Copy always" is redundant. They do two completely different things. (I recommend using the "Content" setting).
Andrew Russell
What I didn't expect is that I also don't need to add the \n in the string for a new line when loading from the XML file. Brilliant.
Sebastian Gray
Andrew, I tested without the Copy Always setting e.g. "Do Not Copy" and received this error message "Cannot find file 'XMLFile1.xml' in the application xap package." - I think it is needed..
Sebastian Gray
TBH why do you want to use this for 4 lines? Isnt it simpler to make a const string or add it to resources? Bear in mind you are usind a mobile and it has a limited battery.
lukas
I did have it as a string and it works for 4 lines which is the example but there is a lot to add. Also I would like to use the same principal for viewing other sets of text, such as the instructions, story line, level introductions etc. Keeping all of this in the code base isn't really desirable. In terms of the relation to battery life, I don't really understand how loading a few XML files would impact the battery life in any significant way - if it did I would be more concerned about the device, if that was the case it would probably need a car battery to receive a phone call :-)
Sebastian Gray
A: 

Why not just write a content pipeline extension and let the content manager worry about it? It's actually pretty easy. This MSDN article explains how.

Here's a blog post that gives an excellent high-level overview.

Andrew Russell
Thanks Andrew, appreciate the suggestion. Bit strange there isn't a simple string loader like this by default.
Sebastian Gray