如何解决“无法识别的元素‘elementName’。” (第x行)(第x行)”?
我有以下代码的
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看您的示例后,我发现它不起作用的原因之一是您正在使用 NameValueFileSectionHandler。如果我没记错的话,这只允许使用以下语法:
根据您想要使用的 xml,您可能需要完全实现配置节类。因此,您将得到如下所示的内容:
然后,这将使用如下所示的 xml:
要点是我正在使用一个集合(您在示例中拥有该集合,实际上是其中的几个)。在该集合中是我所代表的一个对象。因此,要让它解析您拥有的 xml,您必须创建一个部分处理程序来匹配您正在使用的内容。
根据我的示例,您可能希望将 xml 更改为以下内容:
然后您可以使用与我的示例类似的类。尽管您可能会得到您想要的东西,但您将需要使用其他配置项才能使其以这种方式工作。
希望有帮助。
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:
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:
This would then use xml like the following:
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:
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.