在Visual Studio IDE中使用XSD进行XML验证

问题描述:

我知道我以前曾经做过,但是今天不起作用,在任何地方都找不到解释该怎么做的地方.可能是我睡眠不足,但我怀疑是小妖精.

I know I have done this before, but it isn't working today, nor can I find anywhere that explains how to do it. It could be my lack of sleep, but I suspect gremlins.

我有一个XML文档和一个充满XSD的目录来定义它.如何设置Visual IDE通知我验证失败,然后在给定上下文中提供有效标记和属性的智能列表?

I have an XML document and a directory full of XSD's that define it. How do I set the Visual IDE up to notify me of validation failures, and then provide an intellisense list of valid tags and attributes in a given context?

我尝试过的事情:

  • 我已将XSD与XML文档一起添加到项目中.
  • 我已将XSD添加到XML Schema列表中(在XML/Schemas ...菜单项下.)
  • 我什至在XML文档中都包含了schemaLocation和noNamespaceSchemaLocation属性.

Visual Studio仍然没有放弃任何有用的调试或编辑信息.我在2010年和2008年都尝试过(我认为我在2008年之前就做了)

Visual Studio still isn't giving up any useful debugging or editing information. I tried both 2010 and 2008 (I've done it before in 2008 I thought)

更新:我让另一位开发人员尝试了此操作,但对他来说也失败了.他知道他已经用其他XML文档完成了此工作,并使其工作了.然后,我下载了Oxygen XML编辑器,它在相同的XML和XSD文件上也可以正常工作,因此这些文件似乎还不错(或者Oxygen更加宽容/灵活……)

Update: I had another developer try this and it failed for him too. He knows he has done it with other XML documents and had it work. I then downloaded Oxygen XML editor and it worked fine on the same XML and XSD files, so the files seem to be fine (or Oxygen is more forgiving / flexible . . . )

您需要将Visual Studio中的XML文档与您拥有的XSD文件相关联.

You'll need to associate the XML document in Visual Studio with the XSD file you have.

  1. 您应该在XML文档的属性"窗口中看到以下内容:

  1. You should see something like this in your Properties window of the XML document:

在XML模式集编辑器中(单击属性"窗口中模式"文本框中的(...)省略号会打开),您需要确保存在您的模式.此外,请确保已启用该架构的Use列-如果未启用,请单击它-您将获得一个选项下拉列表,并选择带有绿色对勾的Use一个:

In the XML schema set editor (opens when you click on the (...) ellipsis in the "Schemas" textbox in your Properties window) you need to make sure you have your schema present. Also, make sure the Use column for that schema is enabled - if not, click on it - you'll get a drop-down list of options, pick the Use one with the green checkmark:

确保Visual Studio的错误列表"窗口可见(菜单视图>错误列表).这将显示XML和XSD模式定义之间的所有不一致.

Make sure Visual Studio's Error List windows is visible (menu View > Error List). This will show all inconsistencies between XML and XSD schema definitions.

所有这些都准备就绪后,Visual Studio XML编辑器应使用蓝色波浪形突出显示编辑器中XML的问题:

Once all of that is in place, the Visual Studio XML editor should highlight problems with your XML in the editor using blue squigglies: