Load XML File from Project Directory
I have in my WPF project an "assets" folder by the fonts, images and XML files. How can I read XML files from the project directory using C#? Tried it on the Assembly, but the file can not be found. About XAML can very easily download images from that folder.
When I use the follow string, the file can't be found. How can I load my XML开发者_如何学Python file from the project directory?
string stream = "pack://application:,,,/ReferencedAssembly;component/assets/myxml.xml";
Thanks in advance for any help.
One solution to load your file, using Linq to Xml, is:
XDocument myxml = XDocument.Load(@"assets\myxml.xml");
After search in deep and deep web:
Set the "Build Action" property for the XML file to "Embedded Resource".
And use:
XDocument xdoc = XDocument.Load(this.GetType().Assembly.GetManifestResourceStream("ProjectName.FolderName.XmlArchive.xml"));
=D
I've got a very similar issue, I was not able to load file from folder in project structure. All settings were correct, but still it was returning IO error.
All answers abow are probably correct - but in my case it still want to load file. There is a need to change one property more:
- Build Action = Embedded Resource
- Copy to Output Directory = Copy always
Without setting it as "Embedded Resource" it was not reachable from my program. After change it is working.
string strAppPath = AppDomain.CurrentDomain.BaseDirectory; private XDocument xDoc = XDocument.Load(strAppPath +"ActionCol\actionInfo1.xml");
精彩评论