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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3161224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:11:43  来源:igfitidea点击:

XML Validation with XSD in Visual Studio IDE

xmlvisual-studioxsdintellisensexsd-validation

提问by Jim McKeeth

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.

我知道我以前做过这个,但今天不起作用,我也找不到任何解释如何做的地方。可能是我睡眠不足,但我怀疑是小鬼。

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?

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

What I have tried:

我尝试过的:

  • I've added the XSD's to the project with the XML document.
  • I've added the XSD's to the XML Schema list (under XML / Schemas... menu item.)
  • I've even included the schemaLocation and noNamespaceSchemaLocation attributes to the XML document.
  • 我已将 XSD 添加到带有 XML 文档的项目中。
  • 我已将 XSD 添加到 XML 架构列表(在 XML / Schemas... 菜单项下。)
  • 我什至在 XML 文档中包含了 schemaLocation 和 noNamespaceSchemaLocation 属性。

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)

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

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

更新:我让另一位开发人员尝试过这个,但他也失败了。他知道他已经使用其他 XML 文档完成了这项工作,并且可以正常工作。然后我下载了 Oxygen XML 编辑器,它在相同的 XML 和 XSD 文件上运行良好,所以这些文件似乎很好(或者 Oxygen 更宽容/灵活......)

回答by marc_s

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

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

  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 Usecolumn for that schema is enabled - if not, click on it - you'll get a drop-down list of options, pick the Useone 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

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

    XML 属性 >架构

  2. 在 XML 架构集编辑器中(当您在“属性”窗口的“架构”文本框中单击 (...) 省略号时打开),您需要确保您的架构存在。此外,请确保该Use架构的列已启用 - 如果没有,请单击它 - 您将获得一个选项下拉列表,选择Use带有绿色复选标记的选项:

    XML 模式选择器

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

  4. 一旦所有这些都到位,Visual Studio XML 编辑器应该使用蓝色波浪线在编辑器中突出显示 XML 的问题:

    错误示例

回答by Ross McNab

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

您不需要在 Visual Studio 中手动关联这些文件 -如果您同时打开它们并且正确定义了命名空间,它会自动将 XML 文件与 XSD 文件匹配。

To define the namespace:

定义命名空间:

In the XML file's root element:

在 XML 文件的根元素中:

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

In the XSD file's schema element:

在 XSD 文件的架构元素中:

<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

当你有一个 targetNamespace 时,在你的架构中使用类型的注意事项

Because you are specifying a targetNamespacein 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:thisattribute in the above <xs:schema />element).

因为您targetNamespace在架构中指定了 a ,所以对架构中定义的类型的任何引用都需要以命名空间为前缀(这就是我们xmlns:this在上述<xs:schema />元素中添加属性的原因)。

<!-- 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" />

回答by EJA

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.

这里的另一个失败点是 Windows 7“阻止”架构文件...右键单击磁盘上的 xsd 文件,属性 > 常规,如果它被阻止,您将有一个“取消阻止”按钮。这导致我的 XML 验证在 VS2012 中失败。

回答by k3b

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

您的 xsd 是否包含"targetNamespace"/schema/@targetNamespace与您在 xml 中引用的命名空间类似的属性?

Examples:

例子:

XSD:

XSD:

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

XML:

XML:

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

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

另请参阅: XML/XSD 智能感知在 Visual Studio 2010 中不起作用

回答by John Livermore

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.

我遇到了同样的问题,但 VS 已经正确引用了我的架构。原来我试图验证的文件没有“xml”文件扩展名。在我的文件名末尾添加了 .xml,验证开始工作。