读取/写入XML文件的问题 [英] Issue with Reading/Writing into XML Files

查看:98
本文介绍了读取/写入XML文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,该应用程序列出了xml文件中的数据,并且用户可以在此xml文件中添加更多条目.稍后,当再次填充此列表时,将显示较新的条目以及较旧的条目.
我的xml文件位于我当前的项目文件夹&中.它的"BUILD ACTION"设置为"CONTENT"
& 复制到输出目录"是总是复制".
现在,当我运行项目时,此xml文件将复制到"Debug"文件夹中.然后修改并更新Output目录中的此文件,并正确显示所有值.
现在的问题是,当我重新启动项目时,再次将此文件复制到Debug文件夹&中.所有更新都将丢失....
请建议我可以使用相同文件并对其进行修改的方式,以使所做的任何更改都不会丢失...
另外,如果我将文件名指定为(@"C:\ file.xml"),则会显示对象引用未设置为对象的实例"错误.
在此先感谢....

I am developing an application which list the data from an xml file, and user can add more entries into this xml file. Later, when this list is again populated, newer entries will be displayed back along with older ones.
My xml file is in my current project folder, & its "BUILD ACTION" is set as "CONTENT"
& "COPY TO OUTPUT DIRECTORY" is "COPY ALWAYS".
Now, when i run the project, this xml file gets copied into "Debug" folder. And then this file in Output directory gets modified and updated and all values are displayed fine.
Now the problem s that, when i restart the project, again this file is copied to Debug folder & all updates are lost....
Kindly suggest ways in which i can use the same file and modify it, so that no change made is lost...
Also, if i specify the name of file as (@"C:\file.xml"), the it show "Object reference not set to an instance of an object" error.
Thanks in advance....

推荐答案

不要在您的项目中包含XML文件,或对其进行更改,以使其不会自动复制到输出目录.然后,您需要修改您的应用程序,以便它可以处理尚不存在文件的情况,并使用其中的一些默认值来创建它.如果要将配置文件部署到另一台PC时将其包含在应用程序中,则可以在构建设置和部署项目时包括该文件.

如果指定文件的完整路径,然后该文件不存在,则在尝试加载该文件时可能会引发异常.我说这可能是因为这取决于您执行加载的确切方式-有些方式会抛出,而有些则不会.空引用异常听起来像是加载操作返回空值,然后您在使用它之前不检查文档变量是否为空.

同样,这听起来像一个文件,应该存储在App Data或Local App Data文件夹中,而不是与可执行文件一起存储.如果您的应用程序已部署到Program Files目录中,那么在不向应用程序授予不必要的特权的情况下,您将在读写应用程序时遇到问题.
Don''t include the XML file in your project, or change it so that''s it''s not automatically copied to the output directory. Then you''ll need to modify your app so that it can handle the case where the file doesn''t exist yet and create it with some default values in it. If you want to include the config file with your app when you deploy it to another PC then you can include that file when you build the setup and deployment project.

If you specify the full path to a file and then that file doesn''t exist, an exception may thrown when you try to load it. I say may because it depends on the exact way you''re doing the loading - some ways throw and others don''t. A null ref exceptions sounds like the load operation is returning null and then you aren''t checking the document variable for null before using it.

Also this sounds like a file that should be stored in the App Data or Local App Data folder, not alongside the executable. If your app is deployed to the Program Files directory then you''ll have problems reading and writing it without granting unnecessary privileges to your app.


这篇关于读取/写入XML文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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