Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load XML File from Project Directory

Tags:

c#

wpf

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.

like image 688
Matapolo Avatar asked May 27 '11 10:05

Matapolo


People also ask

Where does the XML file path resides on your project?

Commonly, the XML file is located in the first folder.

Where does XML go in Visual Studio project?

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.

How do I open an XML file in Visual Studio?

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.


2 Answers

One solution to load your file, using Linq to Xml, is:

XDocument myxml = XDocument.Load(@"assets\myxml.xml");
like image 186
aligray Avatar answered Sep 22 '22 02:09

aligray


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

like image 32
Helio Passarelli Avatar answered Sep 21 '22 02:09

Helio Passarelli