2

I am using the Visual Studio 2005, and i created one application with "App.config" file. when i try to edit and add new value to that App.config file it shows an error please help me..

My app.config file contains:

<?xml version="1.0" encoding="utf-8" ?>
 <configuration>
  <appSettings>
   <add key="keyvalue" value="value"/>
    <add key="keyvalue1" value="value1"/>
 </appSettings>
 <mySettings>
   <add name="myname" myvalue="value1"/>
 </mySettings>
</configuration>

It shows an error as:

Could not find schema information for the element "mySettings"
Could not find schema information for the element "add"
Could not find schema information for the element "myvalue"
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
Ramesh
  • 1,073
  • 6
  • 24
  • 53
  • 1
    Note these 'errors' are just informational messages. Visual Studio is just letting you know it cannot work out if these values are supposed to be elements, attributes, what type it is supposed to store. It is safe to ignore these, but the answers provided give guidance on how to implement reading these custom values. – Dominic Zukiewicz Mar 28 '12 at 17:46

2 Answers2

6

Don't create a "MySettings" group. Put whatever you need in the AppSettings group.

You COULD create a mySettings group, but if you do include custom (non-standard) configuration sections, you have to declare them in a configSections element as described here or here.

I'd question if it's really necessary, however and go with my first answer unless there's a really good reason for adding custom sections, because it's just better to follow the normal standards. It just makes it easier on future maintenance programmers.

David
  • 72,686
  • 18
  • 132
  • 173
3

You are defining a new section which isn't part of the normal configuration file:

 <mySettings> 
   <add name="myname" myvalue="value1"/> 
 </mySettings> 

To incorporate your own section, you need to write something to read your specific section. You then add a reference to the handler you want to deal with the section like this:

<configuration>
    <configSections>
       <section name="mySettings" type="MyAssembly.MySettingsConfigurationHander, MyAssembly"/>
    </configSections>
    <!-- Same as before -->
</configuration>

An example code sample would be:

public class MySettingsSection
{
     public IEnumerable<MySetting> MySettings { get;set; }
}

public class MySetting
{
    public string Name { get;set; }
    public string MyValue { get;set; }
}

public class MySettingsConfigurationHander : IConfigurationSectionHandler
{
     public object Create(XmlNode startNode)
     {
          var mySettingsSection = new MySettingsSection();

          mySettingsSection.MySettings = (from node in startNode.Descendents()
                                         select new MySetting
                                         {
                                            Name = node.Attribute("name"),
                                            MyValue = node.Attribute("myValue")
                                         }).ToList();

         return mySettingsSection;
     }
}

public class Program
{
    public static void Main()
    {
        var section = ConfigurationManager.GetSection("mySettings") as MySettingsSection;

        Console.WriteLine("Here are the settings for 'MySettings' :");

        foreach(var setting in section.MySettings)
        {
            Console.WriteLine("Name: {0}, MyValue: {1}", setting.Name, setting.MyValue);
        }
    }
}

There are other ways to read the configuration file, but this was the simplist to type freehand.

Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61