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 file from the project directory?
string stream = "pack://application:,,,/ReferencedAssembly;component/assets/myxml.xml";
Thanks in advance for any help.
Commonly, the XML file is located in the first folder.
Add the XML file to Visual Studio Project Open up Visual Studio and create a new project or open an existing one. Add an existing item to the C# project. – Select the XML file we created earlier and press Add. – The file will be added to the project.
On the File menu, point to New, and click File. Select XML File in the Templates pane and click Open. A new file is opened in the editor.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With