ShadowMare
ShadowMare

Reputation: 2097

How to read xml from resource via xmlreader?

I want to read a file.xml in my project (I just want it packed in my .exe) using XmlReader. It works when I read the file from the application folder, but i want to read it from the resources so I can give out my executable without additional files.

I tried different approaches but got none of them to work. I use C# with WPF 3.0.

Can you help me?

Upvotes: 2

Views: 8351

Answers (2)

jschroedl
jschroedl

Reputation: 4986

Add your xml file and mark it in the Properties as an "Embedded Resource". Load it with something like this:

        Assembly a = Assembly.GetExecutingAssembly();
        using (Stream stream = a.GetManifestResourceStream("LoadResourceTest.mydata.xml"))
        using (XmlReader r = XmlReader.Create(stream))
        { 
            ...
        }

My test program is LoadResourceText.exe thus the LoadResourceTest in this code.

Upvotes: 5

MyKuLLSKI
MyKuLLSKI

Reputation: 5325

If your loading anything as a resource you have to use WPF Pack URI's

Some examples:

Absolute Path

"pack://application:,,,/ResourceFile.xaml"
"pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml"

Relative Path

"/ResourceFile.xaml"
"/ReferencedAssembly;component/ResourceFile.xaml" 

Upvotes: 2

Related Questions