Working with Configuration Files – List Items

Continuing my posts about configuration files, now I’m going to show you how to work with a list of items.

Here is the xml:

<?xml version="1.0" encoding="utf-8" ?>
<main>
  <enabled>true</enabled>
  <name>Example text name</name>
  <hosts>
    <host ip="127.0.0.1" port="80" username="test" password="pass" />
    <host ip="172.168.0.1" port="80" username="test2" password="pass2" />
  </hosts>
</main>

Is basically the same xml file I was using in the previous post but now hosts is a list and not only a single entry.

So, let’s see how we handle it in our code:

[XmlRoot("main")]
public class ModelExampleConfiguration
{
    public ModelExampleConfiguration()
    {
        this.Hosts = new List<HostConfiguration>();
    }
        
    [XmlElement("enabled")]
    public bool Enabled { get; set; }

    [XmlElement("name")]
    public string Name { get; set; }

    [XmlArray("hosts")]
    [XmlArrayItem("host")]
    public List<HostConfiguration> Hosts { get; set; }
}

This is important! Note that the list had to be instantiated in the constructor so that the .NET can add the items into the list. Also, it was used to new attributes: XmlArray and XmlArrayItem. The first one defines the name of the list, and the second the name of the items of the list.

You might also need a list where you don’t have a parent item. Let’s see an example:

<?xml version="1.0" encoding="utf-8" ?>
<main>
  <enabled>true</enabled>
  <name>Example text name</name>
  <hosts>
    <host ip="127.0.0.1" port="80" username="test" password="pass" />
    <host ip="172.168.0.1" port="80" username="test2" password="pass2" />
  </hosts>
  <users groupName="admin">
    <user name="user1" />
    <user name="user2" />
  </users>
  <users groupName="guests">
    <user name="user3" />
    <user name="user4" />
  </users>
</main>

I add those two groups of users and each group of user has a list of users. Our full code then will look like:

using System.Xml.Serialization;

namespace ConsoleApplication1
{
    [XmlRoot("main")]
    public class ModelExampleConfiguration
    {
        public ModelExampleConfiguration()
        {
            this.Hosts = new List<HostConfiguration>();
        }
        
        [XmlElement("enabled")]
        public bool Enabled { get; set; }

        [XmlElement("name")]
        public string Name { get; set; }

        [XmlArray("hosts")]
        [XmlArrayItem("host")]
        public List<HostConfiguration> Hosts { get; set; }

        [XmlElement("users")]
        public UserGroupConfiguration[] UserGroup { get; set; }
    }

    public class HostConfiguration
    {
        [XmlAttribute("ip")]
        public string IP { get; set; }

        [XmlAttribute("port")]
        public int Port { get; set; }

        [XmlAttribute("username")]
        public string Username { get; set; }

        [XmlAttribute("password")]
        public string Password { get; set; }
    }

    public class UserGroupConfiguration
    {
        [XmlAttribute("groupName")]
        public string GroupName { get; set; }

        [XmlElement("user")]
        public UserConfiguration[] Users { get; set; }
    }

    public class UserConfiguration
    {
        [XmlAttribute("name")]
        public string Name { get; set; }
    }
}

See that now I used an array instead of a generic list. I could have used the list as well, but I think that way is simpler.

Advertisements

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