Skip to content

Generating an XML Document Using LINQ to XML

Robert Greiner
Robert Greiner
2 min read

In the past, programmaticly creating XML documents in any programming language was extremely tedious and error-prone and .NET was no exception. Fortunately, LINQ remedied this for us in .NET 3.0.

In this post, I will demonstrate how mind-numbingly easy it is to create an XML document using LINQ to XML. Once you do this, you will never want to go back to the old ways of generating XML again.

Let's start by creating an XML document that contains all of the processes a system is running. Fortunately for us, LINQ makes this straightforward.

private static XDocument GetProcesses() {
    return new XDocument(
        new XElement("Processes",
            from p in Process.GetProcesses()
            orderby p.ProcessName ascending
            select new XElement("Process",
                new XAttribute("Name", p.ProcessName),
                new XAttribute("ProcessID", p.Id))));
}

Easy, right? Notice how LINQ even allows us to order the process by their IDs in a way that is easy to read and understand.

Now, if you are used to generating XML documents the old C# 2.0 way then this should be even more exiting because this same bit of functionality would take about four times the amount of code to accomplish. These new LINQ classes allow us do do things much more efficiently and succinctly. Let's dive in to them and learn how they work.

  • **[XDocument]** will contain your entire XML document. You add XElement and XAttribute objects to your XDocument.
  • **[XElement]** holds a single XML element. An XElement object can be added to an XDocument or another XElement object.
  • **[XAttribute]** contains a single attribute name/value pair that is contained within an XElement object.

Once the XDocument is generated, a simple .ToString() will return the entire document to us in a printable format.

XDocument xdoc = GetProcesses();
Console.WriteLine(xdoc.ToString());

If you run this on your machine, you should get something like this:

I uploaded this sample project to my Github account if you want to use it to help you learn these concepts better. The project is released under the Open Source Public License which means you can use it for free in any one of your projects without having to worry about licensing or attribution. Enjoy!

Please consider subscribing, it's free.
Technology

Robert Greiner Twitter

Professional optimist. I write a weekly newsletter for humans at the intersection of business, technology, leadership, and career growth.


Related Posts

Members Public

AI Rule #1 - Customer First

In 1985, Warren Buffett wisely said, "The first rule of investment is don't lose. And the second rule of investment is don't forget the first rule, and that's all the rules there are." Similarly, the first rule of AI investment is to

AI Rule #1 - Customer First
Members Public

Navigating the Upside Down as a Technology Leader

Ideas for leading technology organizations with confidence and creativity.

Navigating the Upside Down as a Technology Leader
Members Public

Artist for a Day

The future of creative output is multiplied by AI.

Artist for a Day