Posted by: Cirilo Meggiolaro | 01/18/2009

Tip of the day #96 – Creating a XML file with LINQ to XML

Let’s check today an easy way to create a XML file using LINQ to XML. If you haven’t checked the Tip#95 it is a good time to do that.

We are going to use the XElement class to achieve that. The XElement holds a XML content file and all its nodes hierarchy. If you think about this hierarchy, it becomes easy to create XML files with a large number of nodes.

For our example, we are going to create a XML file with a list of football players. The structure we want to achieve is:

<?xml version=1.0 encoding=utf-8?>

<Players>

  <Player Number=“”>

    <Name></Name>

    <BirthDate></BirthDate>

    <Team></Team>

    <Position></Position>

  </Player>

</Players>

 

The following xml above contains a root level named Players and may contain one or more Player elements.

 

Each Player element has an attribute called Number and the following elements: Name, Birth Date, Team and Position.

 

How to…

 

Let’s create a root level XElement object and add child XElements to it. The following constructors are available for the XElement class:

 

public XElement(XElement other);

public XElement(XName name);

public XElement(XStreamingElement other);

public XElement(XName name, object content);

public XElement(XName name, params object[] content);

 

The last overload is the one that we are going to use. The first parameter is the element name, and the array of objects will be our node attributes and elements. The following code snippet shows how to create each player separated and add them to the root XElement object:

 

        private static void CreateXmlFile(string path)

        {

            /// Creates a XElement object to store the player 1 information.

            XElement player1 = new XElement(“Player”,

                new XAttribute(“Number”, “39”),

                new XElement(“Name”, “Willie Parker”),

                new XElement(“BirthDate”, “11/11/1980”),

                new XElement(“Team”, “Pittsburgh Steelers”),

                new XElement(“Position”, “RB”));

 

            /// Creates a XElement object to store the player 2 information.

            XElement player2 = new XElement(“Player”,

                new XAttribute(“Number”, “7”),

                new XElement(“Name”, “Ben Roethlisberger”),

                new XElement(“BirthDate”, “3/2/1982”),

                new XElement(“Team”, “Pittsburgh Steelers”),

                new XElement(“Position”, “QB”));

 

            /// Creates a root level element named Players and

            /// add both player 1 and 2 as its elements.

            XElement players = new XElement(“Players”, player1, player2);

 

            /// Save the players xml file.

            players.Save(path);

        }

 

Output:

 

<?xml version=1.0 encoding=utf-8?>

<Players>

  <Player Number=39>

    <Name>Willie Parker</Name>

    <BirthDate>11/11/1980</BirthDate>

    <Team>Pittsburgh Steelers</Team>

    <Position>RB</Position>

  </Player>

  <Player Number=7>

    <Name>Ben Roethlisberger</Name>

    <BirthDate>3/2/1982</BirthDate>

    <Team>Pittsburgh Steelers</Team>

    <Position>QB</Position>

  </Player>

</Players>


Responses

  1. Cool stuff!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: