3

次のコードがあります

var section = new CustomConfigurationSection();
section.SectionInformation.Type = "System.Configuration.NameValueFileSectionHandler";
section.SectionInformation.SetRawXml(sectionXml);
configuration.Sections.Add(sectionName, section);

スローする最後の行:

ConfigurationErrorsException モニターの構成セクション ハンドラーの実行中にエラーが発生しました。

内部例外あり:

要素 'screens' を認識できません。(1行目) (1行目)

CustomConfigurationSection の定義:

public class CustomConfigurationSection: ConfigurationSection
{
    public CustomConfigurationSection()
    {
    }
}

configuration は、「ConfigurationSectionCollection」タイプを持つ Sections という名前のプロパティを持つカスタム クラスのインスタンスです。

そして、sectionXml の着信 xml は次のとおりです。

<monitor>
  <screens>
    <screen>
      <regions>
        <region>
          <labelCoordinates />
          <startupApplication>Internet</startupApplication>
          <color />
          <width>426</width>
          <height>266</height>
          <x1>0</x1>
          <x2>0</x2>
          <y1>0</y1>
          <y2>0</y2>
        </region>
      </regions>
      <height>800</height>
      <width>1280</width>
    </screen>
    <screen>
      <regions />
      <height>0</height>
      <width>0</width>
    </screen>
  </screens>
</monitor>

どうすればこれを機能させることができますか?

4

1 に答える 1

7

あなたの例を見た後、それが機能しないことがわかる理由の1つは、NameValueFileSectionHandlerを使用していることです。私の記憶が正しければ、これは次の構文のみを許可します。

<YourSectionName>
  <add key="monitor.region.x" value="0"/>
<YourSectionName>

使用する xml に基づいて、おそらく構成セクション クラスを完全に実装する必要があります。したがって、次のようなものになります。

class ServiceResponseSection : ConfigurationSection
{
    [ConfigurationProperty("ServiceResponses")]
    [ConfigurationCollection(typeof(ServiceResponse), AddItemName = "addServiceResponse", RemoveItemName = "removeServiceResponse", ClearItemsName = "clearServiceResponses")]
    public ServiceResponses ServiceResponses
    {
        get { return this["ServiceResponses"] as ServiceResponses; }
    }

}

public class ServiceResponses : ConfigurationElementCollection
{
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    public ServiceResponse this[int index]
    {
        get
        {
            return (ServiceResponse)this.BaseGet(index);
        }
        set
        {
            if (this.BaseGet(index) != null)
            {
                this.BaseRemoveAt(index);
            }
            this.BaseAdd(index, value);
        }
    }

    public new ServiceResponse this[string responseString]
    {
        get
        {
            return (ServiceResponse)this.BaseGet(responseString);
        }
        set
        {
            if (this.BaseGet(responseString) != null)
            {
                this.BaseRemoveAt(this.BaseIndexOf(this.BaseGet(responseString)));
            }
            this.BaseAdd(value);
        }
    }

    public void Add(ServiceResponse ServiceResponse)
    {
        this.BaseAdd(ServiceResponse);
    }

    public void Clear()
    {
        this.BaseClear();
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ServiceResponse();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ServiceResponse)element).ResponseString;
    }

    public void Remove(ServiceResponse element)
    {
        BaseRemove(element.ResponseString);
    }

    public void Remove(string responseString)
    {
        BaseRemove(responseString);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }

}

public class ServiceResponse : ConfigurationElement
{
    private int m_tryCount;

    public ServiceResponse()
    {
        this.m_tryCount = 0;
    }

    [ConfigurationProperty("responseString")]
    public string ResponseString
    {
        get { return (String)this["responseString"]; }
        set { this["responseString"] = value; }
    }

    [ConfigurationProperty("matchWholeString")]
    public bool MatchWholeString
    {
        get
        {
            return (bool)this["matchWholeString"];
        }
        set
        {
            this["matchWholeString"] = value.ToString();
        }
    }

    [ConfigurationProperty("retryCount")]
    public int RetryCount
    {
        get
        {
            return (int)this["retryCount"];
        }
        set
        {
            this["retryCount"] = value.ToString();
        }
    }

    [ConfigurationProperty("failsProcess")]
    public bool FailsProcess
    {
        get
        {
            return (bool)this["failsProcess"];
        }
        set
        {
            this["failsProcess"] = value.ToString();
        }
    }

    public int TryCount
    {
        get { return this.m_tryCount; }
        set { this.m_tryCount = value; }
    }

    public void Reset()
    {
        this.m_tryCount = 0;
    }

}

これは、次のように xml を使用します。

    <ServiceResponseList>
    <ServiceResponses>
        <clearServiceResponses/>
        <addServiceResponse responseString="API Server Login Error" matchWholeString="false" retryCount="5" failsProcess="false"/>
    </ServiceResponses>
</ServiceResponseList>

要点は、私がコレクションを使用していることです (あなたの例では、実際にはいくつかあります)。そのコレクションの中には、私が表現しているオブジェクトがあります。したがって、使用している xml を解析するには、使用しているものと一致するようにセクション ハンドラーを作成する必要があります。

私の例に基づいて、おそらくあなたのxmlを次のようなものに変更したいと思うでしょう:

<monitor>
  <screens>
    <screen height="800" width="1280">
      <regions>
         <region startupApplication="Internet" width="426" height="266" x1="0" x2="0" y1="0" y2="0"/>
     </regions>
   </screen>
  </screens>
</monitor>

次に、私の例と同様のクラスを使用できます。おそらくあなたが望むものを手に入れることができますが、そのように動作させるには他の構成項目を使用する必要があります.

それが役立つことを願っています。

于 2009-12-31T18:42:58.357 に答える