验证自定义配置部分中的唯一性

时间:2016-01-18 12:39:27

标签: c# xml validation xsd app-config

我有自定义配置部分的以下xsd,其中LdapServerSection : ConfigurationSectionLdapServerCollection : ConfigurationCollectionLdapServerElement : ConfigurationElement

    <xs:schema attributeFormDefault="unqualified" 
               elementFormDefault="qualified" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               targetNamespace="http://my-namespace.de/LdapServerSection.xsd"
               xmlns:mstns="http://my-namespace.de/LdapServerSection.xsd">
      <xs:element name="ldapServerSection">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ldapServers">
              <xs:complexType>
                <xs:sequence>
                  <xs:element maxOccurs="unbounded" name="ldapServer">
                    <xs:complexType>
                      <xs:attribute name="name" type="xs:string" use="required" />
(...)
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
              </xs:complexType>
              <xs:unique name="UniqueServerName">
                <xs:selector xpath="mstns:ldapServer" />
                <xs:field xpath="@name" />
              </xs:unique>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>

如果我尝试使用重复的服务器名称,就像在此配置摘录中一样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="ldapServerSection" (...) />
  </configSections>
  <ldapServerSection xmlns="http://my-namespace.de/LdapServerSection.xsd">
    <ldapServers>
      <ldapServer name="Development" (...) />
      <ldapServer name="Development" (...) />     
    </ldapServers>
  </ldapServerSection>

Intellisense警告我

  

&#34;有一个重复的键序列&#39;开发&#39;对于&#39; http://my-namespace.de/LdapServerSection.xsd:UniqueServerName&#39;密钥或唯一身份约束。&#34;

虽然这是预期的行为,但是无论验证错误如何,都会加载配置,第二个条目会覆盖第一个条目。

那我怎么能避免这个呢?

0 个答案:

没有答案