Member Avatar for The Road To Voi

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"?>
<Tasks>
   <Task1>
	<Name>Shutdown</Name>
	<Location>C:\WINDOWS\system32\shutdown.exe</Location>
	<Arguments>-s -f -t 30</Arguments>
	<RunWhen>
	    <Time>8:00:00 a.m.</Time>
	    <Date>18/03/2011</Date>
	    <Days>
	        <Monday>false</Monday>
		<Tuesday>false</Tuesday>
		<Wednesday>false</Wednesday>
		<Thursday>false</Thursday>
		<Friday>false</Friday>
		<Saturday>false</Saturday>
		<Sunday>false</Sunday>
		<Everyday>true</Everyday>
		<RunOnce>false</RunOnce>
	    </Days>
	</RunWhen>
	<Enabled>true</Enabled>
    </Task1>
</Tasks>

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

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("Tasks.xml");

XmlNodeList searchNode = xmlDocument.GetElementsByTagName(tagName);
MessageBox.Show(searchNode[0].InnerText);

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

Hi,

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();
doc.Load(reader);
reader.Close();


 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

commented: Thank you, good information :) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.