Adding Controls to an Application with WPF - Creating the XML data file
(Page 2 of 4 )
The controls are positioned correctly, and now we can turn our attention to the tasks in the to-do list. Obviously, the tasks need to be drawn from some data source. Although there are several options available, the easiest way to store and retrieve the data is through a simple XML file. Preparing this file won't require a lot of work, and the result will be readable and manageable.
Each task needs to have a name, a description, a priority, and a status (done or not done). Describing this in XML is very easy. Each task can be represented by a Task element, and inside of each Task element can be elements to describe the properties just listed. So, the XML to describe the task of watering flowers might look something like this:
<Task>
<Name>Water Flowers</Name>
<Priority>Medium</Priority>
<Done>No</Done>
<Description>
The flowers need to be watered, or else they'll die.
</Description>
</Task>
As you can see, we haven't opted for anything complex. We're only making a simple to-do list.
Go ahead and add a new XML file to the project. Name it Tasks.xml and fill it with some tasks:
<?xml version="1.0" encoding="utf-8" ?>
<Tasks>
<Task>
<Name>Water Flowers</Name>
<Priority>Medium</Priority>
<Done>No</Done>
<Description>The flowers need to be watered, or else they'll die.</Description>
</Task>
<Task>
<Name>Eat Breakfast</Name>
<Priority>High</Priority>
<Done>Yes</Done>
<Description>It's the most important meal of the day.</Description>
</Task>
<Task>
<Name>Buy More Ink</Name>
<Priority>Low</Priority>
<Done>No</Done>
<Description>It'll be out eventually.</Description>
</Task>
</Tasks>
By default, the XML file is a resource. This means that the file will be included in the application's resulting assembly. This isn't what we want. In order for the application to function properly, the XML file needs to exist alongside the assembly. That way, it can be modified. To fix this, select the file, and in the Properties Window, change Build Action to Content and Copy to Output Directory to Copy if newer:

This will allow everything to work as intended.
Next: Databinding >>
More Windows Scripting Articles
More By Peyton McCullough