在 VisualStudioIDE 中使用 XSD 进行 XML 验证

我知道我以前这样做过,但是今天它不工作,我也找不到任何地方来解释如何做到这一点。可能是因为我睡眠不足,但我怀疑是小精灵。

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

我所尝试的:

  • 我已经用 XML 文档将 XSD 添加到项目中。
  • 我已经将 XSD 添加到 XML Schema 列表(在 XML/Schemas... 菜单项下)
  • 我甚至将 schemaLocation 和 noNamespaceSchemaLocation 属性包含到 XML 文档中。

VisualStudio 仍然没有放弃任何有用的调试或编辑信息。我在2010年和2008年都试过(我以为我在2008年就试过)

更新: 我让另一个开发人员尝试了这个,他也失败了。他知道自己已经用其他 XML 文档做到了这一点,并且使其工作正常。然后我下载了氧气 XML 编辑器,它在相同的 XML 和 XSD 文件上工作得很好,所以这些文件看起来很好(或者氧气更加宽容/灵活... ...)

103419 次浏览

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

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

    XML Properties > Schema

  2. 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:

    XML Schema Selector

  3. 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.

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

    Example of Error

Does your xsd contain an attribute "targetNamespace" /schema/@targetNamespace that is similar to the namespace you are referencing in the xml?

Examples:

XSD:

<xs:schema .... targetNamespace="Datafile.xsd" ... >

XML:

<data xmlns="Datafile.xsd" >...</data>

See also: XML/XSD intellisense not working in Visual Studio 2010

I had this same problem, but VS was referencing my schema correctly already. Turns out the file I was trying to validate didn't have an 'xml' file extension. Added .xml to the end of my filename, and the validation started to work.

You don't need to manually associate the files in Visual Studio - it will automatically match an XML file to a XSD file if you have them both open, and you have your namespace defined correctly.

To define the namespace:

In the XML file's root element:

<Data xmlns='http://yourdomain.com/yourschema.xsd'>
...
</Data>

In the XSD file's schema element:

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://yourdomain.com/yourschema.xsd"
xmlns:this="http://yourdomain.com/yourschema.xsd"
elementFormDefault="qualified">
...
</xs:schema>

A note on using Types in your schema when you have a targetNamespace

Because you are specifying a targetNamespace in your schema, any references to types defined in the schema will need to be prefixed with a namespace (which is why we added the xmlns:this attribute in the above <xs:schema /> element).

<!-- Define the type as normal -->
<xs:complexType name="Row">
<xs:sequence>
<xs:element name="Name" type="xs:string" minOccurs="1" maxOccurs="1" />
<xs:element name="Value" type="xs:float" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
<!-- Use the type (note the "this:" prefix) -->
<xs:element name="Row" type="this:Row" minOccurs="0" maxOccurs="unbounded" />

Another point of failure here is Windows 7 "blocking" schema files... right-click on the xsd file on disk, Properties > General and if it's blocked, you'll have an "Unblock" button. This was causing my XML validation to fail in VS2012.