Read xml file in Android/Mono

2019-08-17 06:18发布

问题:

I'm trying to read an xml file which I plan to include as part of my mobile application. I've created a folder called "XML" and added a file called "test.xml". The files build action is set to content. When I run the app it crashes on trying to find the file. Here is the code I'm using:

_path = "XML/test.xml";
doc = new XmlDocument();
doc.Load(_path);

Here is the error I get:

System.IO.DirectoryNotFoundException has been thrown.

Could not find a part of the path "/XML/test.xml"

Do I need to register the directory/folder somehow? Thanks for any advice, I'm sure I've just missed something simple...

回答1:

Do you mean that you'd created XML directory inside of your project?

If you want to include XML to your APK - one of the best ways will be to create /res/xml/file.xml and read it inside application with Resources class. Please have a look at this thread - maybe you'll find it helpful. Open XML file from res/xml in Android Good luck