.Net parse ConfigurationSection выдает исключение - PullRequest
0 голосов
/ 17 октября 2019

Я пишу пользовательский раздел конфигурации, например,

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="devices"
             type="TestNewSyntactic.DevicesConfig, TestNewSyntactic" />
  </configSections>
  <devices>
    <bureau id="10">
      <center>
        <ddt unit_id="152">
          <dev devid="2" name="appsvr" app="TrainPlanForm">
            <ip1> 172.27.80.1 </ip1>
            <ip2> 172.27.81.1 </ip2>
          </dev>
        </ddt>
      </center>
    </bureau>
  </devices>
</configuration>

, и мой код раздела и элемента имеет вид

public class ConfigurationInnerElement<T> : ConfigurationElement
{
    protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
    {
        Value = (T) reader.ReadElementContentAs(typeof(T), null);
    }

    public T Value { get; private set; }
}
public class DevicesConfig : ConfigurationSection
{
    [ConfigurationProperty("bureau")]
    public BureauElement Bureau => base["bureau"] as BureauElement;
}

public class BureauElement : ConfigurationElement
{
    [ConfigurationProperty("id", IsRequired = true)]
    public byte BureauCode => Convert.ToByte(base["id"]);

    [ConfigurationProperty("center", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(CenterCollection), AddItemName = "ddt")]
    public CenterCollection Center => base["center"] as CenterCollection;
}

[ConfigurationCollection(typeof(DdtDeviceCollection), AddItemName = "ddt")]
public class CenterCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new DdtDeviceCollection();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((DdtDeviceCollection)element).Id;
    }
}
public class DeviceElement : ConfigurationElement
{
    [ConfigurationProperty("devid", IsRequired = true, IsKey = true)]
    public ushort DeviceId => Convert.ToUInt16(base["devid"]);

    [ConfigurationProperty("name", IsRequired = true)]
    public string Name => base["name"] as string;

    [ConfigurationProperty("app", IsRequired = true)]
    public string ApplicationName => base["app"] as string;

    [ConfigurationProperty("ip1")]
    public ConfigurationInnerElement<string> IP1 => base["ip1"] as ConfigurationInnerElement<string>;

    [ConfigurationProperty("ip2")]
    public ConfigurationInnerElement<string> IP2 => base["ip2"] as ConfigurationInnerElement<string>;
}
public class DdtDeviceCollection : ConfigurationElementCollection
{
    [ConfigurationProperty("unit_id")]
    public ushort Id { get; set; }
    protected override ConfigurationElement CreateNewElement()
    {
        return new DeviceElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((DeviceElement) element).DeviceId;
    }
}

Но когда я получаю GetSection («устройства»), онвсегда выдает исключение, сказав, что

System.Configuration.ConfigurationErrorsException: 'Нераспознанный элемент' dev '

Я так запутался, почему элемент "dev" не распознан,пожалуйста, помогите мне

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...