So I've made a Task Scheduling application (for learning purposes). The program currently saves all the data in simple text form. I use Regex to split the data up and assign it to the appropriate variables.

I don't really like this way of doing things as it seems messy and it's not very clean looking code wise. I think learning some XML interaction would be a good way to extend my knowledge.

This is the way I would like my XML file layout to be:

<?xml version="1.0"?>
	<Arguments>-s -f -t 30</Arguments>
	    <Time>8:00:00 a.m.</Time>

I have worked out how to read the XML file using this method:

XmlDocument xmlDocument = new XmlDocument();

XmlNodeList searchNode = xmlDocument.GetElementsByTagName(tagName);

So if the XML contain multiple Tasks (tagged as Task1, Task2 etc) would this be an effective way of loading the data into my Tasks class (using a List<> that contains multiple tasks)? Or is there and easier way?

Also when it comes to saving the stored Tasks I need to take the values data the Tasks class and save it as an XML File, this means if I've added another Task the XML file will have to have another Task[index] tag like in the XML file above? Is there a way of doing this nicely? Some pointer would be great.

Any help is appreciated :D


It seems a good approach and pretty straight forward; this way you can create a loop that reads the tasks into your classes and at the end writes the tasks back into the XML file.

You will have to write a class that reads the XML file and extract the class, somethig like that this:

XmlTextReader reader = reader = new XmlTextReader(FileName);
XmlDocument doc = new XmlDocument();

 XmlNodeList NodeList;
XmlElement root = doc.DocumentElement;
NodeList = root.SelectNodes("/Tasks");

//At this point you should have the nodes listed

foreach (XmlElement element in NodeList)
    //create a reader class that creates a task for each element in NodeList

Hope it helps

Thank you, good information :)
This article has been dead for over six months. Start a new discussion instead.