Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Parsing - Read a Simple XML File and Retrieve Values

Tags:

c#

.net

parsing

xml

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:

  1. Open Tasks.xml
  2. Load the first Task tag.
  3. In that task retrieve the values of the Name, Location and Arguments tags.
  4. Then open the RunWhen tag and retrieve the values of the Time and Date tags.
  5. After that open the Days tag and retrieve the value of each individual tag within.
  6. Retrieve the value of Enabled.
  7. 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.

like image 360
Lucidity Avatar asked Apr 09 '11 10:04

Lucidity


People also ask

What is parsing in XML with example?

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.

What is parsing XML?

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.

What are the two methods of parsing in XML document?

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.


1 Answers

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);     } } 
like image 104
Serghei Avatar answered Oct 12 '22 11:10

Serghei