LINQ To XML Example

In this example I’m showing how you can use LINQ to query data and port the output to XML format. This is one of many many ways you can output data. C# .NET has many ways to port data to many types of formats. This is just one way.

 
Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Xml.Linq;
using System.Diagnostics;

namespace MyLinq
{
    class Program
    {
        /// <summary>
        /// Demonstrating an example of LINQ to XML. I'm attempting to take my employee information
        /// and output it to an XML formatted variable to display.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            QueryXml();
            QueryEmployee();

            Console.ReadLine();
        }

        //Private Methods
        //
        private static void QueryEmployee()
        {
            Console.WriteLine("****Demonstrating Pulling Data From A List Query Via LINQ To XML Data****");

            // Creating am IEnumerable list of Employee type that I can use LINQ on.
            List<Employee> employees = new List<Employee>()
            {
                new Employee { ID = 1, Name = "Daniel", HireDate = new DateTime(2002, 3, 5) },
                new Employee { ID = 2, Name = "Sara", HireDate = new DateTime(2002, 10, 15) },
                new Employee { ID = 3, Name = "Jack", HireDate = new DateTime(2007, 10, 11) },
                new Employee { ID = 4, Name = "Robert", HireDate = new DateTime(2010, 4, 22) }
            };

            // Creating a LINQ query with lazy operators.
            IEnumerable<Employee> query =
                from e in employees
                where e.HireDate.Year < 2005
                orderby e.Name
                select e;

            // Adding a new employee to the list after initialization to show that when the collection is iterated
            // thru this employee object will show.
            employees.Add(new Employee { ID = 5, Name = "Paul", HireDate = new DateTime(2002, 2, 10) });

            // Pulling the query data into XML with some LINQ Lambda Expressions.
            XElement employeeXmlElements = new XElement("EmployeeNames", query.Select(i => new XElement("Employee", i.Name)));
            Console.WriteLine(employeeXmlElements);

            Console.WriteLine();
        }

        private static void QueryXml()
        {
            Console.WriteLine("****Demonstrating Simple XML Output");
            
            // Using XElement to create XML data.
            XElement students =
                new XElement("students",
                    new XElement("student", "Daniel"),
                    new XElement("student", "Sara"),
                    new XElement("student", "Thomas")
                    );

            Console.WriteLine(students);
            Console.WriteLine();
        }
    }
}

This example is using the same code as the previous posts. INSERT LINKS TO POSTS. You will notice that the QueryEmployee method is doing the same thing as previous posts. With the exception that I’m using an XElement object to build the XML formatted data and storing it. In the XElement constructor I’m using LINQ lambda expressions to build the XML tree.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s