开发者

How to save XML file before update?

Right so I have a simple app that consists of a calendar a Set Event button and list box that populates u开发者_如何学Pythonsing a DataTemplate in WPF. When I build the app the Events.xml file is like this:

<?xml version="1.0" encoding="utf-8" ?>
    <events>

    </events>

I add events to xml through code. Final structure of the xml file is the following

<?xml version="1.0" encoding="utf-8" ?>
<events>
   <event Name="Some Name">
       <startDate>20.03.2010</startDate>
       <endDate>29.03.2010</endDate>
   </event>
</event>

Now here's my problem. If I update the app and deploy it with Click Once and the app updates it self, I lose the list because the new file is empty. Any workaround this?

Here's how I add the Data:

var xDocument = XDocument.Load(@"Data\Events.xml");
xDocument.Root.Add(new XElement("event", new XAttribute("name", event.Name),
                new XElement("startDate", startDate),
                new XElement("endDate", endDate)
                )
                );
xDocument.Save(@"Data\Events.xml");


If it is an option I would suggest that you don't include the Events.xml as a part of the project. If you did this, you could:

  1. Check to see if it is there before you load it.
  2. If it is not there, write out an empty one.

If Events.xml is not a part of the project, Click Once won't overwrite it on updates.


I figured out my own problem.

Here's my method. Create a folder @C:\Program Files\<MyApp>\Data\ add the Events.xml file there when the app runs for the first time and I have the file there always.

The only draw back is that user needs to delete the folder manually on uninstall.

Click Once does not install the app @C:\Program Files\<MyApp>\ by default. It installs to @C:\Users\<Username>\AppData\Local\Apps\2.0\ (under Windows 7).

Any workaround this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜