WPF C#XML文件添加为资源 [英] WPF C# XML file added as resource

查看:594
本文介绍了WPF C#XML文件添加为资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用添加到我的项目具有以下属性的XML文件:

 生成操作:资源
复制到输出目录:复制始终

然后可执行文件运行时,我需要的用户能够编辑XML(添加,删除和修改节点)。是否可以 ?或者,我需要生成操作改变的内容?

感谢

编辑:
我已经删除并重新添加为内容。
但现在我有另外一个问题:当我把它添加到项目中,它看起来它在应用程序根目录(WpfApplication1 \\ WpfApplication1 \\ myfile.xml中)。
但后来当我运行的应用程序,负责该函数添加一个节点工作正常,但将文件保存到WpfApplication1 \\ WpfApplication1 \\ BIN \\调试\\目录!因此,真实myfile.xml中不更新

我使用的是:

  XMLHosts.Load(将myfile.xml);
XMLHosts.Save(myfile.xml中);

和数据源声明为:

 < XmlDataProvider X:键=MyfileData来源=myfile.xml中的XPath =书籍/书/>


解决方案

如果您想从磁盘加载的XML(和修改),利用生成操作=无和复制=复制始终。

你会遇到一个问题是,如果你安装你的应用程序程序文件,你将无法编辑该文件在Windows Vista / 7,除非你运行的管理。 - 程序文件是安装过程中唯一可编辑的

一个很好的做法是复制XML到%APPDATA%\\您的应用程序(APPDATA =环境变量,采用统环境访问)。您可以preform这个副本无论在安装或首次运行时。
%APPDATA%是为运行时可修改的设置。

如果您标记您的XML作为无+ CopyAlways,您可以从执行目录复制到%APPDATA%\\你在应用程序启动的应用程序,如果它还没有在目的地地址的存在(如果尚未复制)。

您可以再从%APPDATA%\\您的应用程序加载和读取最新的设置。

I'm using an XML file added into my project with following properties :

Build Action : Resource
Copy to output directory : Copy Always

Then when the executable is running, I need the users to be able to edit the XML (add, remove and modify nodes). Is it possible ? Or do I need to change the Build Action to Content ?

Thanks

edit : i've removed it and added again as Content. But now I have another issue : When I add it to the project, it looks for it in the app root directory (WpfApplication1\WpfApplication1\myfile.xml). But then when I run the app, the function in charge to add a node works fine, but it saves the file to the WpfApplication1\WpfApplication1\bin\Debug\ directory ! Hence the "real" myfile.xml is not updated .

I'm using :

XMLHosts.Load("myfile.xml");
XMLHosts.Save("myfile.xml");

and the datasource is declared as :

<XmlDataProvider x:Key="MyfileData"  Source="myfile.xml" XPath="Books/Book" />

解决方案

If you want the xml to be loaded from disc (and modified), use Build Action = None and Copy = Copy Always.

An issues you will run into is that if you install your app to "Program Files", you will not be able to edit the file on Windows Vista/7 unless you running as admin. - "Program Files" is only editable during install.

A good practice would be to copy xml to %APPDATA%\Your app (APPDATA = environmental variable, access using System.Environment). You can preform this copy either during install, or during first run. %APPDATA% is meant for runtime changeable settings.

If you mark your xml as None+CopyAlways, you can copy it from execution directory to %APPDATA%\Your app on application startup, if it does not yet exist at destination address (if not yet copied).

You can then load it from %APPDATA%\Your app and read the latest settings.

这篇关于WPF C#XML文件添加为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆