如何解决“无法识别的元素‘elementName’。” (第x行)(第x行)”?

发布于 2024-08-16 16:11:43 字数 1575 浏览 7 评论 0原文

我有以下代码的

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

最后一行抛出:

ConfigurationErrorsException 错误 执行配置时发生 监视器的部分处理程序。

除了内部例外:

无法识别的元素“屏幕”。 (线 1)(第1行)

CustomConfigurationSection 的定义:

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

configuration 是自定义类的实例,该类具有名为“Sections”的属性,其类型为“ConfigurationSectionCollection”。

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

我怎样才能让它工作?

I have the following code

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

last line of which throws:

ConfigurationErrorsException An error
occurred executing the configuration
section handler for monitor.

with the inner exception:

Unrecognized element 'screens'. (line
1) (line 1)

Definition of CustomConfigurationSection:

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

configuration is an instance of a custom class, which has a property named Sections, that have the type 'ConfigurationSectionCollection'.

And the incoming xml in sectionXml is:

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

How can I get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溇涏 2024-08-23 16:11:43

查看您的示例后,我发现它不起作用的原因之一是您正在使用 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>

然后您可以使用与我的示例类似的类。尽管您可能会得到您想要的东西,但您将需要使用其他配置项才能使其以这种方式工作。

希望有帮助。

After looking at your example one reason I can see that it won't work is you are using the NameValueFileSectionHandler. If I remember correctly this only allows the following syntax:

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

Based on the xml you are wanting to use you probably need to fully implement the config section classes. So you would have something like the following:

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

}

This would then use xml like the following:

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

The main point is that I am using a collection (which you have in your example, actually a couple of them). Within that collection is an object that I am representing. So to get it to parse the xml you have you would have to make a section handler to match what you are using.

Based on my example you would probably want to change your xml to something along the line of:

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

You could then use classes similar to my example. Although you could probably get what you are wanting but you will need to use other configuration items to get it to work that way.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文