Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Validation with XSD in Visual Studio IDE

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

like image 217
Jim McKeeth Avatar asked Jul 01 '10 19:07

Jim McKeeth


People also ask

How do I reference an XSD file in XML?

Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.


2 Answers

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

like image 52
marc_s Avatar answered Oct 19 '22 15:10

marc_s


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" /> 
like image 30
Ross McNab Avatar answered Oct 19 '22 16:10

Ross McNab