views:

131

answers:

1

I have an XML configuration file with a collection of strings like so:

<SomeSetting>value</SomeSetting>
<SomeOtherSetting>value</SomeOtherSetting>
<TypesOfThings>
  <Thing>Type 1</Thing>
  <Thing>Type 2</Thing>
  <Thing>Type 3</Thing>
</TypesOfThings>

These 'Things' become options in the front end of the app for the user to choose from.

My question is do I have to create a thing class in order to use Serialization properly or is there a way of using attributes to read the strings straight into a list?

For example (bodies and private vars removed for brevity, this is .NET 2.0):

[Serializable]
public class Config
{
   public string SomeSetting
   { 
        get;
        set;
   }


   public string SomeOtherSetting
   { 
        get;
        set;
   }

   public List<string> TypesOfThings
   {
      get;
      set;
   }
}
+1  A: 

You specify the way it should be serialized using the XmlArrayAttribute :

[XmlArray(ElementName = "TypesOfThings")]
[XmlArrayItem(ElementName="Thing")]
public List<string> TypesOfThings
{
   get;
   set;
}

Edit: the name of items is actuality specified using the XmlArrayItemAttribute

Pop Catalin