Enabling Xml Schema Validation in Visual Studio
Enabling Xml Schema Validation in Visual Studio
The post is a simple tip
which will help you to
enable Xml schema
validation and intellisense
while writing an Xml file
inside Visual Studio.
The Problem
Writing Xml files according to a specific Xml schema definition is
sometime very hard and can be a very “painful” task for a developer.
You need to remember all the small details and elements in order to do it
properly. So how can we validate the Xml files while writing them?
The Solution
Having the Xsd file which can validate your schema during the writing
of the Xml file can be very helpful. Also, getting hints and intellisense
about Xml properties, elements and errors while writing the Xml file
can increase the writing speed of the file.
Visual Studio include this feature you just need to know how to enable
it and use it.
Step 1
Add a namespace to your schema file. For example the following
schema fragment has a “urn:schema-namespace” namespace:
<xs:schema targetNamespace="urn:schema-namespace" xmlns="urn:schema-namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:NS="urn:schema-namespace">
<!-- Your schema definitions goes here -->
</xs:schema>
Having the namespace is a must because in Visual Studio you’ll
have to give the schema namespace to the Xml file you write.
Step 2
Put the Xsd file in the following directory while Visual Studio is
closed:
$Visual Studio Directory$\Xml\Schemas
(Replace the $Visual Studio Directory$ with the location of your
Visual Studio)
Step 3
Open Visual Studio and start writing your Xml file. Remember that you
have to give the namespace of the schema in order to have schema
validations and intellisense inside Visual Studio.
The following example is using the schema namespace from step 1:
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="urn:schema-namespace">
<!-- write here your Xml -->
</root>
Summary
Lets sum up, I showed a very helpful tip of how to enable Xml
schema validation and intellisense through Visual Studio. I use this
feature a lot when I write Xml files which have schema files.
I hope this tip will help you too.