105

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?

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.

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)

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

Jim McKeeth
  • 38,225
  • 23
  • 120
  • 194

5 Answers5

145

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Thanks, that is a great detailed explanation, but mine still isn't working. I didn't know you could get to the schemas list from properties like that. – Jim McKeeth Jul 01 '10 at 20:49
  • @Jim McKeeth: what if you modify your XML and save it again? Sometimes the XML validation needs a trigger to kick in... – marc_s Jul 01 '10 at 21:15
  • I've accepted your answer even though it didn't work for me. I am guessing my system was just hosed, but I have moved to a new computer and everything works fine now. – Jim McKeeth Oct 21 '10 at 21:19
  • 42
    @jim Not sure if you had the same problem, but I spent a ridiculous amount of time before I realized that the XML file must be open in the editor and the focus must be **in the editor** when you call up the Properties window. Only then do you get the "XML Document" properties where you can assign a schema. If the focus is in the Solution Explorer, the Properties window displays file properties only, i.e. no schema assignment is possible there. – herzbube Jun 05 '12 at 12:31
  • Actually @herzbube solved my issue. That focus out thing was simply not working for me too. – Asif Ashraf Jun 23 '13 at 00:05
  • @marc_s It works nice.i was looking for xml validation in visual studio.But if i open a xml file and if there is no xsd file in the path(folder) then visual studio will not detect xsd.Is there any option by which visual studio gives message like xsd file not found – IT researcher Aug 06 '13 at 12:06
  • @ITresearcher Visual Studio doesn't have the ability to detect missing XSD files from the installation directory, because it doesn't know what XSD files are missing once all of the XSD files are loaded. Instead, it assumes all of the XSD files in the `%installroot%/xml/schema` are loaded. This is something you have to manually add/remove/replace/repair/modify/etc. outside of Visual Studio. – tom_mai78101 Feb 27 '17 at 13:34
  • 2
    I think you should add @herzbube's findings in the answer itself. – ashish Feb 16 '18 at 12:11
  • Another important point is to make sure that the Error List tool window is not filtering out anything -- the validation error only appeared when the list was set to include Warnings and the Intellisense source was included (i.e. not Build Only). – JDHnz Apr 14 '19 at 23:10
35

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" />
Ross McNab
  • 11,337
  • 3
  • 36
  • 34
3

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.

EJA
  • 1,335
  • 1
  • 9
  • 11
1

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
k3b
  • 14,517
  • 7
  • 53
  • 85
0

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.

John Livermore
  • 30,235
  • 44
  • 126
  • 216