[英]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. 我想使用XmlReader读取项目中的file.xml(我只希望将其打包在我的.exe中)。 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. 我在WPF 3.0中使用C#。
Can you help me? 你能帮助我吗?
Add your xml file and mark it in the Properties as an "Embedded Resource". 添加您的xml文件,并将其在“属性”中标记为“嵌入式资源”。 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. 我的测试程序是LoadResourceText.exe,因此是此代码中的LoadResourceTest。
If your loading anything as a resource you have to use WPF Pack URI's 如果您将任何内容作为资源加载,则必须使用WPF Pack URI
Some examples: 一些例子:
"pack://application:,,,/ResourceFile.xaml"
"pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml"
"/ResourceFile.xaml"
"/ReferencedAssembly;component/ResourceFile.xaml"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.