如何在iOS的MonoTouch /一夫一妻制应用程序包中读取XML资源文件?

时间:2023-01-12 18:57:02

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.

使用MonoTouch/一夫一妻制,我试图在我的应用程序包中包含一个定制的XML文件,以便在我的代码中使用,但是我不熟悉正确的方式。我已经将XML文件包含在我的项目中,并将其标记为“Content”,以便将其复制到app bundle。

In this case, I want to load some map data in XML I have defined (the XML format 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).

在这种情况下,我希望用我定义的XML加载一些映射数据(XML格式是指定的,所以我不能更改它),同时保留在PC上运行XNA下相同代码的能力。我还没有幸运地在iOS上找到合适的机制。我本以为会出现以下内容,但是mapPath出现了null(可能我只是误解了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?

任何建议吗?

3 个解决方案

#1


0  

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

我的MT项目中有标记为内容的XML文件。我直接将它们加载到一个XDocument中:

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.

这个文件只是包含在应用程序包中,而不是像传统的。net资源那样嵌入到可执行文件中。

#2


0  

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

我使用XDocument和Streams在不同的应用程序中读取XML数据。正如前面提到的,关键是要确保文件被标记为“内容”。

#3


0  

Use TitleContainer.OpenStream, it is cross platform friendly.

使用TitleContainer。OpenStream,跨平台友好。

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.
  • 注意,如果您的资源位于子文件夹中,则需要在整个文件夹路径中传递。

#1


0  

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

我的MT项目中有标记为内容的XML文件。我直接将它们加载到一个XDocument中:

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.

这个文件只是包含在应用程序包中,而不是像传统的。net资源那样嵌入到可执行文件中。

#2


0  

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

我使用XDocument和Streams在不同的应用程序中读取XML数据。正如前面提到的,关键是要确保文件被标记为“内容”。

#3


0  

Use TitleContainer.OpenStream, it is cross platform friendly.

使用TitleContainer。OpenStream,跨平台友好。

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.
  • 注意,如果您的资源位于子文件夹中,则需要在整个文件夹路径中传递。