How to read in a txt file in XNA 4 for Windows Phone 7?

自作多情 提交于 2019-12-10 10:36:46

问题


I had a look at this previous question however this doesn't seem to work for XNA 4 for a Windows Phone 7 project: 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.


回答1:


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'.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/3743722/how-to-read-in-a-txt-file-in-xna-4-for-windows-phone-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!