Highlight: LINQ to XML

Post Saturday, 29th March 2008 - Posted by Tom Bell

For my upcoming instant messaging project I will need to create a C# windows service for the Peer Mediator servers. To look into windows services using C# I decided to create a Cron-like service for scheduling. I decided the the crontab configuration file should be an XML file. I figured I could then use the new LINQ to XML technology in the .NET 3.5 framework. Below is an example of loading my crontab XML file using LINQ to XML in C#.

XDocument crontab = XDocument.Load("CronTab.xml"); var jobs = from job in crontab.Descendants("cronjob") select new { Schedule = job.Element("schedule").Value, Task = job.Element("task").Value }; foreach (var job in jobs) { Console.WriteLine("Schedule: " + job.Schedule); Console.WriteLine("Task: " + job.Task); }

LINQ is a great new technology in the 3.5 version of the .NET framework. You can also use LINQ to SQL which is similar except querying data from a SQL database, and LINQ to Objects which allows you to query collections of objects.

Tags tags: xml linq .net