1

I am trying to have the following section structure in my app.config

<MyConfig>
    <NewsFeeds site="abc">
        <add name="first" />
    </NewsFeeds>
    <NewsFeeds site="zyx">
        <add name="another" />
    </NewsFeeds>
</MyConfig>

I already have the MyConfig section working but I am unsure of how the NewsFeed collection should be coded, or if this structure is even possible. Right now I have the following classes so far:

[ConfigurationCollection(typeof(NewsFeedConfig))]
public class NewsFeedConfigCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new NewsFeedConfig();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((NewsFeedConfig)(element)).Name;
    }

    public NewsFeedConfig this[int idx] { get { return (NewsFeedConfig)BaseGet(idx); } }
}

public class NewsFeedConfig : ConfigurationElement
{
    [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }

    [ConfigurationProperty("source", IsRequired = true)]
    public string Source
    {
        get { return (string)base["source"]; }
        set { base["source"] = value; }
    }
}

However, this requires that all news feeds be under one news feed collection, and then i'd have to parse them out manually by adding a Site property to each element. That's fine, but is it possible to do it in a way that the XML defined above would work?

KallDrexx
  • 27,229
  • 33
  • 143
  • 254

1 Answers1

0

I think you'll find your answer over here: Sections must only appear once per config file! why?

You may want to restructure your xml to something more like:

<MyConfig>
  <NewsFeed site="abc">
    <Feeds>
      <Feed name="first" />
    </Feeds>
  </NewsFeed>
  <NewsFeed site="zyx">
    <Feeds>
      <Feed name="second" />
      <Feed name="third" />
      <Feed name="fourth" />
    </Feeds>
  </NewsFeed>
</MyConfig>
Community
  • 1
  • 1
Jonathan
  • 4,916
  • 2
  • 20
  • 37