2013-08-22 49 views
0

我有自定義System.Configuration.ConfigurationSection派生類,它代表我的app.config文件中的配置部分。對於該XML文檔,我也有xsd模式文檔。配置文件具有以下(簡化)結構:ConfigurationSection各種名稱和模式驗證

<?xml version="1.0" encoding="utf-8" ?> 

<configuration> 

    <configSections> 

    <section name="sectionOne" type="/*...*/" /> 
    <section name="sectionTwo" type="/*...*/" /> 
    <section name="sectionThree" type="/*...*/" /> 

    </configSections> 

    <sectionOne xmlns="http://tempuri.org/MySchema.xsd"> 

    <Container> 

     /*...*/ 

    </Container> 

    </sectionOne > 

    <sectionTwo xmlns="http://tempuri.org/MySchema.xsd"> 

    <Container> 

     /*...*/ 

    </Container> 

    </sectionTwo > 

    <sectionThree xmlns="http://tempuri.org/MySchema.xsd"> 

    <Container> 

     /*...*/ 

    </Container> 

    </sectionThree > 

</configuration> 

正如我們所看到的,我有一個類型的幾個部分,出於各種目的,我檢索使用ConfigurationManager類配置數據:

ConfigurationManager.GetSection(sectionName); 

由於節名稱不是常量字符串值,因此xsd模式僅驗證根元素的子元素(從Container標籤開始)。因此,在VS2012中,錯誤列表工具欄,我得到了以下信息:

Could not find schema information for the element 'http://tempuri.org/MySchema.xsd:SectionOne'. 

Could not find schema information for the element 'http://tempuri.org/MySchema.xsd:SectionTwo'. 

Could not find schema information for the element 'http://tempuri.org/MySchema.xsd:SectionThree'. 

如何解決驗證機制。

+1

這是一個已知的bug: http://stackoverflow.com/questions/179927/how-to-resolve-could-not-find-schema-information-for-the-element-attribute-xxx – 2013-08-22 12:26:27

+0

它不能解決我的問題,因爲將來我的庫將被其他用戶使用,他們可能會根據需要重命名配置節,並且不應強制它們修改'xsd'模式定義。 – sgnsajgon

回答

1

沒有涉及更改XSD的解決方案:無法編寫與任意名稱匹配的元素的模式。必須明確指定所有可能的有效元素名稱,因此爲了正確驗證段元素的名稱,必須將其添加到http://tempuri.org/MySchema.xsd模式或DotNetConfig.xsd