Visual Studio总是选择错误的xsd文件用于App.config。

24

首先,我有一个.NET 4.0应用程序,并使用以下配置:

<?xml version="1.0"?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
</configuration>
我遇到了和这个问题一样的困扰:app.config "Could not find schema information" after converting to Visual Studio 2010 / .Net 4.0 :在编辑器中未能识别supportedRuntime元素,导致出现提示信息。
然后我按照答案所说做了:进入app.config编辑器并前往“属性”窗口,选择“模式”。
我取消选中DotNetConfig.xsd并改为选择DotNetConfig35.xsd,但是VS总是会自动再次添加DotNetConfig.xsd。即使我明确将其设置为“不使用此模式”,或者甚至从列表中删除该模式 - 它也会自动重新添加到列表中并被选中。
由于我现在选择了两个定义相同元素的模式,因此会得到很多警告。
如何更改模式以使用DotNetConfig35.xsd,并且不让DotNetConfig.xsd自动再次添加?
3个回答

45
我将问题解释如下:文件DotNetConfig.xsd<startup>元素定义错误(或不完整)。所有DotNetConfig.xsdDotNetConfig35.xsdDotNetConfig30.xsdDotNetConfig20.xsd文件的第230行包含此问题。
<xs:element name="startup" vs:help="configuration/startup" />

另一方面,微软将 启动设置架构 描述为非空元素。因此,我建议在 DotNetConfig.xsd 和所有 DotNetConfigXX.xsd 文件中替换上述行,这些文件位于 %ProgramFiles%\Microsoft Visual Studio 10.0\Xml\Schemas 目录(或者在 64 位系统上的 %ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Xml\Schemas 目录)中,并使用以下行:

<xs:element name="startup" vs:help="configuration/startup">
    <xs:complexType>
        <xs:choice minOccurs="1" maxOccurs="1">
            <xs:element name="requiredRuntime" vs:help="configuration/startup/requiredRuntime">
                <xs:complexType>
                    <xs:attribute name="version" type="xs:string" use="optional" />
                    <xs:attribute name="safemode" type="xs:boolean" use="optional" />
                </xs:complexType>
            </xs:element>
            <xs:element name="supportedRuntime" minOccurs="1" maxOccurs="unbounded" vs:help="configuration/startup/supportedRuntime">
                <xs:complexType>
                    <xs:attribute name="version" type="xs:string" use="optional" />
                    <xs:attribute name="sku" type="xs:string" use="optional" />
                </xs:complexType>
            </xs:element>
        </xs:choice>
        <xs:attribute name="useLegacyV2RuntimeActivationPolicy" type="xs:boolean" use="optional" />
        <!-- see http://msdn.microsoft.com/en-us/library/bbx34a2h.aspx -->
    </xs:complexType>
</xs:element>

在这样的修改和重启Visual Studio 2010之后,您将不会再遇到您描述的警告。当然,人们可以更详细地定义所有属性或元素的模式(特别是如果我们发现关于<startup>部分更详细的文档),但我只想描述问题的原因和解决问题的一种方法。

顺便提一下,在同一个目录中的catalog.xml文件的内容将决定选择DotNetConfig.xsdDotNetConfig35.xsd和其他DotNetConfigXX.xsd文件,该文件的模式在此处进行了描述。标准版本的catalog.xml文件包含以下行:

<Association extension="config" schema="%InstallRoot%/xml/schemas/dotNetConfig20.xsd" condition="starts-with($TargetFrameworkMoniker, '.NETFramework,Version=v2.')" />
<Association extension="config" schema="%InstallRoot%/xml/schemas/dotNetConfig30.xsd" condition="starts-with($TargetFrameworkMoniker, '.NETFramework,Version=v3.0')" />
<Association extension="config" schema="%InstallRoot%/xml/schemas/dotNetConfig35.xsd" condition="starts-with($TargetFrameworkMoniker, '.NETFramework,Version=v3.5')" />
<Association extension="config" schema="%InstallRoot%/xml/schemas/dotNetConfig.xsd"   condition="starts-with($TargetFrameworkMoniker, '.NETFramework,Version=v4.') or $TargetFrameworkMoniker = ''" />

这样,所有扩展名为.config的文件将被视为具有上述文件之一描述的XSD模式的文件。


1
谢谢。那解决了问题。我甚至没有想到 VS 附带的 xsd 可能是错误的可能性。 - Sebastian P.R. Gingter
@Sebastian P.R. Gingter:不用客气!没有人是完美的,包括微软。为所有 .config 文件(如 app.config、web.config 等)制定一个 XSD 不是好主意。因此,配置模式存在其他类似问题。尽管如此,我很高兴能帮到你。祝你一路顺风! - Oleg
为我解决了41个警告。非常感谢 :) - Øyvind Bråthen
我的电脑不允许我保存文件。 - user2602079
如果您想修改Program Files中的文件,您应该拥有管理员权限。 - Oleg
显示剩余7条评论

1
当我遇到这个问题时,取消勾选模式框的原因是由于有多个Visual Studio实例打开。(我同时打开了一个带有一个项目的VS2015和一个不同项目的VS2013。) 需要注意的是,多个版本的Visual Studio并且在同一个项目/解决方案上来回切换也似乎是一些重复架构问题出现的原因。

0

这是一篇旧文章 - 但我遇到了同样的问题。

我采取的方法与Ken Johnsrude上面建议的相同 - 创建一个新的.xsd文件:

http://w3stack.org/question/c-how-to-fix-error-could-not-find-schema-information-for-the-attributeelement-by-creating-schema/

  1. MSVS > 打开项目 app.config

  2. XML > 创建模式

    这将在 %TEMP% 中创建 "app.xsd"。

     例如:c:\users\paulsm\AppData\Local\Temp\app.xsd
    
  3. 将 app.xsd 移动到项目目录中

  4. App.Config,右键单击 > 属性 > 模式 > ... app.xsd > 使用 > 使用此模式 = 是


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接