0

I want a configuration section that looks like this:

<MailMessage>
  <from value="me@you.com" />
  <subject value ="Subject goes here" />
  <body value="Hello. You've got mail!" />
</MailMessage>

And I have implemented the classes like in the second answer of this link: How to implement a ConfigurationSection with a ConfigurationElementCollection

Now for me the elements of MailMessage section are not a collection but this should not be a problem but I receive the error when I try to access the property:

Unrecognized element 'from'

I get the section with the code:

private static MailMessageSection emailSection = ConfigurationManager.GetSection("MailMessage") as MailMessageSection;

Here is the implementation of the elements:

public class MailMessageSection : ConfigurationSection
{

    [ConfigurationProperty("from")]
    public FromElement From
    {
           get { return base["from"] as FromElement; }
    }
    [ConfigurationProperty("subject")]
    public SubjectElement Subject
    {
        get { return base["subject"] as SubjectElement; }
    }
    [ConfigurationProperty("body")]
    public BodyElement Body
    {
        get { return base["body"] as BodyElement; }
    }

}
public class FromElement : ConfigurationElement
{
   [ConfigurationProperty("value")]
    public string From
    {
        get { return base["value"] as string; }
    }
}
public class SubjectElement : ConfigurationElement
{

    [ConfigurationProperty("value")]
    public string Subject
    {
        get { return base["value"] as string; }
    }
}
public class BodyElement : ConfigurationElement
{

    [ConfigurationProperty("value")]
    public string Body
    {
        get { return base["value"] as string; }
    }
}

Any ideas what could be wrong? Thanks for your time!

UrsulRosu
  • 507
  • 1
  • 6
  • 16

1 Answers1

1

Looking for error is serializable classes can be frustrating. I suggest you to use the auto generate functionalities in VisualStudio. Here is how you do it (very simple):
1. Copy the XML example (to the clipboard)
2. Create new class for the XML ("MailMessageSection" in your case)
3. In VS go to Edit > Paste Special > Paste XML As Classes

I know this is not exactly the reason why the from is not working, but using auto generated code is much better practice then write it on your own.

Hope it helps...

Natan Braslavski
  • 819
  • 8
  • 18