1

I have a App.config file, where I declared a section group ListOfItems and 2 sections Item_A and Item_B:

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
  <configSections>
    <sectionGroup name="ListOfItems">
      <section name="Item_A" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      <section name="Item_B" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7"/>
  </startup>
  <ListOfItems>
    <Item_A>
      <add key="FileAddress" value="http://www.example.com/file1.xml"/>
      <add key="FileType" value="xml"/>
      <add key="DateFieldFormat" value="yyyy/MM/dd"/>
      <add key="MainFolder" value="Folder_A"/>
      <add key="OutputFolder" value="output"/>
      <add key="ArchiveFolder" value="archive"/>
    </Item_A>
    <Item_B>
      <add key="FileAddress" value="http://www.example.com/file2.txt"/>
      <add key="FileType" value="txt"/>
      <add key="DateFieldFormat" value="yyyy-MM-dd"/>
      <add key="MainFolder" value="Folder_B"/>
      <add key="OutputFolder" value="output"/>
      <add key="ArchiveFolder" value="archive"/>
    </Item_B>
  </ListOfItems>
</configuration>

Then I defined a class with properties:

Namespace DataTypes
Friend Class ItemObject
    Inherits ConfigurationSection       

    <ConfigurationProperty("FileAddress", DefaultValue:=Nothing, IsRequired:=True)>
    Public Property FileAddressProperty As String
        Get
            Return CType(Me("FileAddress"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("FileAddress") = value
        End Set
    End Property

    <ConfigurationProperty("FileType", DefaultValue:="xml", IsRequired:=True)>
    Public Property FileTypeProperty As String
        Get
            Return CType(Me("FileType"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("FileType") = value
        End Set
    End Property

    <ConfigurationProperty("DateFieldFormat", DefaultValue:="yyyy/MM/dd", IsRequired:=True)>
    Public Property DateFieldFormatProperty As String
        Get
            Return CType(Me("DateFieldFormat"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("DateFieldFormat") = value
        End Set
    End Property

    <ConfigurationProperty("MainFolder", DefaultValue:="Folder", IsRequired:=True)>
    Public Property MainFolderProperty As String
        Get
            Return CType(Me("MainFolder"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("MainFolder") = value
        End Set
    End Property

    <ConfigurationProperty("OutputFolder", DefaultValue:="output", IsRequired:=True)>
    Public Property OutputFolderProperty As String
        Get
            Return CType(Me("OutputFolder"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("OutputFolder") = value
        End Set
    End Property

    <ConfigurationProperty("ArchiveFolder", DefaultValue:="archive", IsRequired:=True)>
    Public Property ArchiveFolderProperty As String
        Get
            Return CType(Me("ArchiveFolder"), String)
        End Get
        Protected Friend Set(ByVal value As String)
            Me("ArchiveFolder") = value
        End Set
    End Property

End Class
End Namespace

Now I have looked here and here, but I cannot figure out how to write the following loop: Foreach item in ListOfItems, create an ItemObject with data from App.config (I would add it to a list of ItemObjects. So I would have a list with 2 items in it, one for Item_A and one for Item_B

Yuropoor
  • 354
  • 2
  • 17

0 Answers0