2012-03-28 5 views
2

Visual Studio 2005を使用していて、「App.config」ファイルで1つのアプリケーションを作成しました。 私はそのファイルをApp.configファイルに新しい値を編集して追加しようとしたとき、それはエラーを示して私を助けてください..CでApp.configファイルを構成する方法

私のapp.configファイルには含まれています

<?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> 

それはようなエラーが表示されます。

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" 
+1

これらの「エラー」は単なる情報メッセージです。 Visual Studioは、これらの値が要素、属性、格納される予定の型であると想定されているとうまくいかないことを知らせるだけです。これらを無視することは安全ですが、提供された回答は、これらのカスタム値の読み込みを実装する方法に関するガイダンスを提供します。 –

答えて

6

"MySettings"グループを作成しないでください。 AppSettingsグループに必要なものを入れてください。

mySettingsグループを作成する必要がありますが、カスタム(非標準)構成セクションを含める場合は、hereまたはhereのようにconfigSections要素で宣言する必要があります。

しかし、本当に必要なのか疑問に思っていましたが、カスタムセクションを追加する本当の理由がない限り、私の最初の答えと一緒に行くことにしました。将来のメンテナンスプログラマーにとっては簡単になります。あなたは、通常のコンフィギュレーションファイルの一部ではない、新しいセクションを定義している

+0

ありがとう@DavidStratton .. – Ramesh

3

:自分のセクションを組み込むには

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

を、あなたはあなたの特定のセクションを読んで何かを記述する必要があります。あなたは、あなたは、このようなセクションに対処したいハンドラへの参照を追加します。

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

例のコードサンプルは次のようになります。

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); 
     } 
    } 
} 

設定ファイルを読み込むための他の方法がありますが、これはフリーハンドタイプの単純化されたものでした。

+0

+1努力を重ねてコードを見せてください。 – David

+0

@Dominic Zukiewiczさん、ありがとうございました..それは私にとって有益です。 – Ramesh

関連する問題