How I use XDocument.Load() to get a file in the application directory

我与影子孤独终老i 提交于 2019-12-20 01:44:33

问题


I have a XML file in a folder in the project. I would like to use XDocument.Load(string) but do I have to write the complete path to the application as part of the URI (such as in my example)?

XDocument xml = XDocument.Load("c:/users/myuser/documents/visual studio
2010/Projects/ErrorRegistro/Registro.xml");

回答1:


Another way that bypass the ressource issue is to add the file to project (same as the ressource solution), then click on the file and in the property tab choose "copy always" in copy to output path. That way the file will get copied to the output directory when you build and it's as simple as doing

 XDocument xml = XDocument.Load("Registro.xml");



回答2:


It would be better to add your XML file as a project resource: go to the properties of the project then to Resources tab. Choose Add Resource and add your file. Then you will be able to get your XDocument that way:

XDocument xml = XDocument.Parse(Properties.Resources.Registro);

If you want, however, to keep everything like you did you could maybe go with Reflection. See the answer here: How to read embedded resource text file



来源:https://stackoverflow.com/questions/18680386/how-i-use-xdocument-load-to-get-a-file-in-the-application-directory

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