开发者

How to read an XML resource file from my app bundle in MonoTouch / MonoGame for iOS?

Using MonoTouch/MonoGame, I am trying to include a custom XML file in my app bundle for use in my code, but I'm not familiar with the right way to go about it. I have included the XML file in my project and marked it as "Content" so it should be copied over to the app bundle.

In this case, I want to load some map data in XML I have defined (the XML fo开发者_开发百科rmat is prescribed, so I can't change it), while preserving the ability to also run the same code under XNA on the PC. I haven't been very lucky at finding the right mechanism on iOS yet. I would have expected something like the following, but the mapPath is coming up null (probably I'm just misunderstanding the correct use of the API).

#if WINDOWS
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.Maps." + map + ".xml");
#else
string mapPath = NSBundle.MainBundle.PathForResource(map,"xml");
Stream s = new FileStream(mapPath, FileMode.Open, FileAccess.Read);
#endif 

Any tips?


I have XML files flagged as content in my MT project. I load them into an XDocument directly:

XDocument doc = XDocument.Load("states.xml");

The file is just contained within the app bundle, not embedded in the executable the way it would be with a traditional .NET resource.


I use both XDocument and Streams to read in XML data in various apps. As already mentioned, the key is making sure your file is marked as "Content".


Use TitleContainer.OpenStream, it is cross platform friendly.

Example:

string xml = new System.IO.StreamReader(TitleContainer.OpenStream("MyApp.Maps." + map + ".xml")).ReadToEnd();
  • Note that you will need to pass in the full folder path if your resource is in a sub-folder.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜