Associating XSD schema with XML document

I have multiple XML documents included in a Visual Studio project.

I also have an XSD schema for each type of these XML documents.

I want to associate the XML documents with its appropriate XSD schema.

When I do this it adds my schema to a hard-coded central repository of schemas.

What I want is to specify a relative path, so that when I check my Visual Studio project in, the associations and xsds are checked in and versioned along with it.

Is there a way to specify within the XML file which XSD it should use, with a relative path

 Thanks in advance.

 



Answer this question

Associating XSD schema with XML document

  • Joao Cunha Lopes

    You can specify relative path the same way:

    <myElement xmlns="foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="foo ..\temp\foo.xsd"/>



  • BJT

    Yes, you can specify the location of your schema in the Xml file. Below is an example:

    <myElement xmlns="foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="foo c:\temp\foo.xsd"/>

    xmlns="foo" attribute specifies what namespace myElement belongs to and xsi:schemaLocation attribute contains two strings - namespace and location (which can contain a relative path). If your schema doesn't have a namespace then you'll need to use xsi:noNamespaceSchemaLocation attribute.

    Stan Kitsis



  • _ross

    A related question.

    If the XML file is empty (no table yet), but the schema location is specified, will loading the file to an XMLDocument automatically create the schema

    Just hoping to clean-up some of my code that tries to load the XML file, then checks if it's null then load the schema from XSD instead.


  • KS

    Thanks for your response Stan. A quick further question...

    "c:\temp\foo.xsd" is an absolute path. How can I specify a relative path like "..\temp\foo.xsd"

    The trouble is that when people check-out the codebase from cvs, the tree of files isn't always located at the same absolute position. Any links between source files need to be relative.


  • Associating XSD schema with XML document