I've written a Task Scheduling program for learning purposes. Currently I'm saving the scheduled tasks just as plain text and then parsing it using Regex. This looks messy (code wise) and is not very coherent.
I would like to load the scheduled tasks from an XML file instead, I've searched quite a bit to find some solutions but I couldn't get it to work how I wanted.
I wrote an XML file structured like this to store my data in:
<Tasks> <Task> <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> </Task> </Tasks>
The way I'd like to parse the data is like so:
- Open Tasks.xml
- Load the first Task tag.
- In that task retrieve the values of the Name, Location and Arguments tags.
- Then open the RunWhen tag and retrieve the values of the Time and Date tags.
- After that open the Days tag and retrieve the value of each individual tag within.
- Retrieve the value of Enabled.
- Load the next task and repeat steps 3 -> 7 until all the Task tags in Tasks have been parsed.
I'm very sure you can do it this way I just can't work it out as there are so many different ways to do things in XML I got a bit overwhelmed. But what I've go so far is that I would most likely be using XPathDocument and XPathNodeIterator right?
If someone can show me an example or explain to me how this would be done I would be very happy.
XML parser is a software library or a package that provides interface for client applications to work with XML documents. It checks for proper format of the XML document and may also validate the XML documents. Modern day browsers have built-in XML parsers. The goal of a parser is to transform XML into a readable code.
Definition. XML parsing is the process of reading an XML document and providing an interface to the user application for accessing the document. An XML parser is a software apparatus that accomplishes such tasks.
Following are the various types of parsers which are commonly used to parse XML documents. Dom Parser − Parses an XML document by loading the complete contents of the document and creating its complete hierarchical tree in memory. SAX Parser − Parses an XML document on event-based triggers.
Easy way to parse the xml is to use the LINQ to XML
for example you have the following xml file
<library> <track id="1" genre="Rap" time="3:24"> <name>Who We Be RMX (feat. 2Pac)</name> <artist>DMX</artist> <album>The Dogz Mixtape: Who's Next?!</album> </track> <track id="2" genre="Rap" time="5:06"> <name>Angel (ft. Regina Bell)</name> <artist>DMX</artist> <album>...And Then There Was X</album> </track> <track id="3" genre="Break Beat" time="6:16"> <name>Dreaming Your Dreams</name> <artist>Hybrid</artist> <album>Wide Angle</album> </track> <track id="4" genre="Break Beat" time="9:38"> <name>Finished Symphony</name> <artist>Hybrid</artist> <album>Wide Angle</album> </track> <library>
For reading this file, you can use the following code:
public void Read(string fileName) { XDocument doc = XDocument.Load(fileName); foreach (XElement el in doc.Root.Elements()) { Console.WriteLine("{0} {1}", el.Name, el.Attribute("id").Value); Console.WriteLine(" Attributes:"); foreach (XAttribute attr in el.Attributes()) Console.WriteLine(" {0}", attr); Console.WriteLine(" Elements:"); foreach (XElement element in el.Elements()) Console.WriteLine(" {0}: {1}", element.Name, element.Value); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With