如何解决“找不到元素/属性<xxx>的模式信息”问题?

47
在Visual Studio中,我有一个使用MS Enterprise Library 4.0应用程序块的asp.net 3.5项目。当我的web config文件打开时,我的错误列表会填满99条消息,例如:
Could not find schema information for the element 'dataConfiguration'.  
Could not find schema information for the attribute 'defaultDatabase'.  
Could not find schema information for the element 'loggingConfiguration'.   
Could not find schema information for the attribute 'tracingEnabled'.   
Could not find schema information for the attribute 'defaultCategory'.  
如果我关闭Web.config文件,这些警告就会消失(但是只要我需要再次打开该文件,它们就会再次出现)。
经过一番探索,我发现这是因为 Visual Studio 需要一个 XSD 或架构文件来“理解” web.config 文件中的模式并为其提供智能感知功能。有人知道如何向 VS 提供适当的架构信息,或者如何关闭这些警告吗?
@Franci - 感谢您的信息,我尝试了那个工具以及 MMC 快照(它们往往会破坏 Web.config 的格式),但它们仍然无法解决我收到的恼人警告。谢谢您的尝试。

嘿,抱歉我不能帮助你。如果你想让我删除我的答案,这样你的问题就会显示为未回答,请在答案下留言,这样我下次登录时就能看到它。 - Franci Penov
我只是重新启动了我的Visual Studio,问题就解决了。 - Saad Farooq
6个回答

31
我基于当前的 app.config 创建了一个新方案,使消息消失。 我只是在 Visual Studio 中使用了"创建模式"按钮,然后就为我创建了一个 xsd 模式。
将该模式保存到适当的位置,并查看 app.config 文件的"属性"选项卡,在那里有一个名为 Schemas 的属性。如果您单击更改按钮,您可以选择同时使用原始的 dotnetconfig 模式和您自己新创建的模式。

在XSD中,您可能需要添加minOccurs="0",这样当您移动/删除部分时,例如<applicationSettings><Company.MyApplication.Settings.Properties>,就不会收到验证警告。 - yzorg
5
需要翻译的内容:Just want to add that this "Schema" property you can access only editing the xml file (app.config) and not from browsing Solution Explorer。只想补充一点,你只能通过编辑XML文件(app.config),而不能从浏览解决方案资源管理器中访问此“Schema”属性。 - Roman Pokrovskij
6
我按照您上述的描述创建了一个XSD模式。这确实帮助我摆脱了“找不到架构信息”的警告消息,但现在我收到了 app.xsd 中的警告“全局元素'configuration'已经被声明过了”。有人知道如何解决吗? - George
3
在Visual Studio 2010中,要找到“创建模式”按钮,首先需要打开“app.config”以启用XML工具,然后将鼠标悬停在工具栏上的按钮上。如果仍然找不到它,请右键单击工具栏,并确保选择了“XML编辑器”。您还可以使用菜单“XML..创建模式”来进行操作。 - Contango
这是否也适用于 ASP.NET MVC 项目的 web.config?我已将创建的 xsd 文件添加到我的 web.config 的模式中,但错误仍未消失。 - Giles Roberts

13

在EntLib 5中包含有XSD,并已安装在Visual Studio模式目录中。在我的情况下,它可以在以下路径找到:

"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Xml\Schemas\EnterpriseLibrary.Configuration.xsd"

CONTEXT

  • Visual Studio 2010
  • Enterprise Library 5

STEPS TO REMOVE THE WARNINGS

  1. 打开您在Visual Studio项目中的app.config文件。
  2. 在XML文档编辑器中右键单击,选择“属性”。
  3. 添加完全限定路径到“EnterpriseLibrary.Configuration.xsd”。

ASIDE

值得重申的是,在打开app.config文件时才会显示这些“错误列表”、“消息”(“无法找到元素的模式信息”)。如果你点击“关闭所有文档”并进行编译...将不会报告任何消息。


超级简单的解决方案。如果您在打开属性后单击路径变量右侧的省略号按钮,那么它甚至更简单 - 这会弹出一个对话框,在其中您可以轻松地单击要包含的XML模式... - DaveN59

11

我使用EntLib配置工具配置了app.config,并设置了LoggingConfiguration块。然后我把这个配置复制到了DotNetConfig.xsd中。当然,它不包括所有属性,只有我添加的属性,但它不再显示那些烦人的信息消息。

<xs:element name="loggingConfiguration">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="listeners">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" name="add">
              <xs:complexType>
                <xs:attribute name="fileName" type="xs:string" use="required" />
                <xs:attribute name="footer" type="xs:string" use="required" />
                <xs:attribute name="formatter" type="xs:string" use="required" />
                <xs:attribute name="header" type="xs:string" use="required" />
                <xs:attribute name="rollFileExistsBehavior" type="xs:string" use="required" />
                <xs:attribute name="rollInterval" type="xs:string" use="required" />
                <xs:attribute name="rollSizeKB" type="xs:unsignedByte" use="required" />
                <xs:attribute name="timeStampPattern" type="xs:string" use="required" />
                <xs:attribute name="listenerDataType" type="xs:string" use="required" />
                <xs:attribute name="traceOutputOptions" type="xs:string" use="required" />
                <xs:attribute name="filter" type="xs:string" use="required" />
                <xs:attribute name="type" type="xs:string" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="formatters">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="add">
              <xs:complexType>
                <xs:attribute name="template" type="xs:string" use="required" />
                <xs:attribute name="type" type="xs:string" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="logFilters">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="add">
              <xs:complexType>
                <xs:attribute name="enabled" type="xs:boolean" use="required" />
                <xs:attribute name="type" type="xs:string" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="categorySources">
        <xs:complexType>
          <xs:sequence>
            <xs:element maxOccurs="unbounded" name="add">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="listeners">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="add">
                          <xs:complexType>
                            <xs:attribute name="name" type="xs:string" use="required" />
                          </xs:complexType>
                        </xs:element>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
                <xs:attribute name="switchValue" type="xs:string" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="specialSources">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="allEvents">
              <xs:complexType>
                <xs:attribute name="switchValue" type="xs:string" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
            <xs:element name="notProcessed">
              <xs:complexType>
                <xs:attribute name="switchValue" type="xs:string" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
            <xs:element name="errors">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="listeners">
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name="add">
                          <xs:complexType>
                            <xs:attribute name="name" type="xs:string" use="required" />
                          </xs:complexType>
                        </xs:element>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:sequence>
                <xs:attribute name="switchValue" type="xs:string" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="name" type="xs:string" use="required" />
    <xs:attribute name="tracingEnabled" type="xs:boolean" use="required" />
    <xs:attribute name="defaultCategory" type="xs:string" use="required" />
    <xs:attribute name="logWarningsWhenNoCategoriesMatch" type="xs:boolean" use="required" />
  </xs:complexType>
</xs:element>

这个很棒!!!感谢这个技巧。现在我知道如何为远程配置制作一个了。 - StingyJack
我将第三行的顺序改为“全部”,以便各个部分可以以任何顺序出现。 - StingyJack
@user57433 - EntLib配置工具和DotNetConfig.xsd文件位于哪里? - Brian McCarthy
3
工作量太大了。你只需要打开app.config文件的属性,点击路径变量,然后点击省略号按钮。这会弹出一个对话框,在其中可以选择要包含的任何模式。 - DaveN59

1
我遇到的“无法找到元素的模式信息”问题,解决方法是:
  • 打开我的app.config文件。
  • 在编辑窗口中右键单击并选择属性
  • 在属性框中,有一行称为模式,我点击了该行并选择了出现在该行中的浏览...框。
  • 对于所有包含我的项目的行以及我正在使用的当前版本的.Net,我只需勾选使用框即可。例如:DotNetConfig30.xsd
之后一切都正常工作了。
我不确定如何取消选中那些包含我的项目的模式行,但当我确保它们被选中时,我就恢复了业务。

1

你尝试将模式文件复制到VS的XML Schema缓存文件夹中了吗?您可以通过查看VS Tools / Options / Test Editor / XML / Miscellaneous来找到该文件夹的位置。不幸的是,我不知道MS Enterprise Library 4.0的模式文件在哪里。

更新:安装MS Enterprise Library后,似乎没有.xsd文件。但是,有一个用于编辑配置的工具-EntLibConfig.exe,您可以使用它来编辑配置文件。此外,如果您向配置文件添加适当的配置部分,则VS应该能够正确解析配置文件。(EntLibConfig会为您添加这些内容,或者您可以自己添加)。以下是loggingConfiguration部分的示例:

<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>

你还需要在你的项目中添加对应程序集的引用。


0

前往此处: NLog xsd文件

下载适用于您的项目的相应xsd文件,并将其保存在NLog.config文件旁边

第一个对我起了作用。


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