如何在 iOS 版 MonoTouch / MonoGame 中从我的应用程序包中读取 XML 资源文件?
使用 MonoTouch/MonoGame,我尝试在我的应用程序包中包含一个自定义 XML 文件以在我的代码中使用,但我不熟悉正确的方法。我已将 XML 文件包含在我的项目中并将其标记为“内容”,因此应将其复制到应用程序包中。
在这种情况下,我想加载一些我定义的 XML 格式的地图数据(XML 格式是规定的,所以我无法更改它),同时保留在 PC 上的 XNA 下运行相同代码的能力。我还没有很幸运地在 iOS 上找到正确的机制。我本来期望类似下面的内容,但是 mapPath 为空(可能我只是误解了 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
有什么建议吗?
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 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).
#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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的 MT 项目中有标记为内容的 XML 文件。我直接将它们加载到 XDocument 中:
该文件仅包含在应用程序包中,而不是像传统 .NET 资源那样嵌入到可执行文件中。
I have XML files flagged as content in my MT project. I load them into an XDocument directly:
The file is just contained within the app bundle, not embedded in the executable the way it would be with a traditional .NET resource.
我使用 XDocument 和 Streams 在各种应用程序中读取 XML 数据。
如前所述,关键是确保您的文件被标记为“内容”。
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".
使用TitleContainer.OpenStream,跨平台友好。
示例:
Use TitleContainer.OpenStream, it is cross platform friendly.
Example: