How to Write an XML File Using LINQ to XML

Many examples exist online for how to read an XML file using LINQ to XML. Unfortunately, there are not many articles describing how to write an XML file using LINQ to XML. In this article, I will demonstrate an easy way to quickly generate a well-formed and valid XML file using LINQ to XML.

The code below demonstrates how easy it is to write an XML file:

Write an XML File Using LINQ to XML

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    /*
    <?xml version="1.0" encoding="utf-8" ?>
    <Persons>
        <Person>
            <PersonID></PersonID>
            <Name></Name>
            <Phone></Phone>
            <Street></Street>
            <City></City>
            <State></State>
            <Zipcode></Zipcode>
        </Person>
    </Persons>
     */

    class Program
    {
        static void Main(string[] args)
        {
            // create an XML file using the structure described above
            XDocument xdoc = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("List of Persons"),
                new XElement("Persons",
                    from xlist in CreateList()
                    select new XElement("Person",
                        new XElement("PersonID", xlist.PersonID),
                        new XElement("Name", xlist.Name),
                        new XElement("Phone", xlist.Phone),
                        new XElement("Street", xlist.Street),
                        new XElement("City", xlist.City),
                        new XElement("State", xlist.State),
                        new XElement("Zipcode", xlist.Zipcode))
                    )
                );

            // save the document
            xdoc.Save(@"c:temp2sample.xml");
        }

        /// <summary>
        /// Creates a list of random persons
        /// </summary>
        private static List<clsPerson> CreateList()
        {
            int i = 0;

            return new List<clsPerson>
               {
                    new clsPerson
                       {
                           PersonID = i++,
                           Name = "Bob Jones",
                           Phone = "555-2345",
                           Street = "123 Nowhere Ave",
                           City = "Nashville",
                           State = "TN",
                           Zipcode = 12345
                       },
                   new clsPerson
                       {
                           PersonID = i++,
                           Name = "Mary Jones",
                           Phone = "555-4321",
                           Street = "123 Nowhere Ave",
                           City = "Nashville",
                           State = "TN",
                           Zipcode = 12345
                       },
                   new clsPerson
                       {
                           PersonID = i++,
                           Name = "John Doe",
                           Phone = "555-7543",
                           Street = "356 Adams Street",
                           City = "Washington",
                           State = "DC",
                           Zipcode = 43678
                       },
                   new clsPerson
                       {
                           PersonID = i,
                           Name = "Jane Doe",
                           Phone = "555-9834",
                           Street = "356 Adams Street",
                           City = "Washington",
                           State = "DC",
                           Zipcode = 43678
                       }
               };
        }
    }

    /// <summary>
    /// Holds information about a person
    /// </summary>
    class clsPerson
    {
        public int PersonID { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public int Zipcode { get; set; }
    }
}

Notice in the code above, everything is done with the XDocument object. As we create the XDocument object, we create the underlying structure of the XML file using the parameters of the XDocument object. You must pass in the parameters in the order you want them to appear in the XML file.

Notice also the use of the XComment. This is an optional element, but can be useful to help explain the contents or purpose of the XML document.

 

Example XML File Output

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!–List of Persons–>
<Persons>
  <Person>
    <PersonID>0</PersonID>
    <Name>Bob Jones</Name>
    <Phone>555-2345</Phone>
    <Street>123 Nowhere Ave</Street>
    <City>Nashville</City>
    <State>TN</State>
    <Zipcode>12345</Zipcode>
  </Person>
  <Person>
    <PersonID>1</PersonID>
    <Name>Mary Jones</Name>
    <Phone>555-4321</Phone>
    <Street>123 Nowhere Ave</Street>
    <City>Nashville</City>
    <State>TN</State>
    <Zipcode>12345</Zipcode>
  </Person>
  <Person>
    <PersonID>2</PersonID>
    <Name>John Doe</Name>
    <Phone>555-7543</Phone>
    <Street>356 Adams Street</Street>
    <City>Washington</City>
    <State>DC</State>
    <Zipcode>43678</Zipcode>
  </Person>
  <Person>
    <PersonID>3</PersonID>
    <Name>Jane Doe</Name>
    <Phone>555-9834</Phone>
    <Street>356 Adams Street</Street>
    <City>Washington</City>
    <State>DC</State>
    <Zipcode>43678</Zipcode>
  </Person>
</Persons>

share save 171 16 How to Write an XML File Using LINQ to XML

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Leave a Reply