Namespaces, schemas, and one confusing system...

Squirm

Well-known member
Joined
May 21, 2001
Messages
139
Location
London, England
User Rank
{modbadge}
So Ive written my XML schema. VS likes it, and the xsd tool creates nice serializable classes, so the schema must be ok. I have called my namespace urn:xmlns:toffee like so:

Code:
<xs:schema id="Toffee" targetNamespace="urn:xmlns:toffee" elementFormDefault="qualified" xmlns="urn:xmlns:toffee" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    ....
</xs:schema>

Now I want to test the schema in Sample.xml, so I started with this:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<userfile xmlns="urn:xmlns:toffee" xmlns:toffee="urn:xmlns:toffee" toffee:schemaLocation="Toffee.xsd">
    ....
</userfile>

userfile is the document/root element defined in the schema, and the code generated by xsd reflects that. However, the code above causes the following error when I open the xml file in VS:

Loading the target schema failed because the file {0} does not exist.
Continue to load data without the referenced schema?

(Despite the yes/no question the only buttons provided are OK and Help!)

Changing the xmlns attribute of the userfile tag to "Toffee.xsd" causes the XML file to load without error, but intellisense for the document is incorrect since Toffee.xsd is not a namespace, just a filename. Do I really have to use the name of the xsd file as the namespace name?

Argh! Its frustrating. All I want to do is have a namespace called urn:xmlns:toffee defined in the schema Toffee.xsd and have it referenced in Sample.xml such that I dont have to prefix every tag with toffee:

Ive read and reread all the tutorials I could find on XML schemas, and I just dont get it.

:confused:

Thanks for reading.
 
You should define your schema as an xml document

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="toffee" elementFormDefault="qualified" xmlns="toffee" xmlns:xs="http://www.w3.org/2001/XMLSchema">
....
</xs:schema>


To associate the schema with a namespace:

<?xml version="1.0" encoding="utf-8" ?>
<t:userfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="toffee" xsi:schemaLocation="toffee Toffee.xsd">
....
</t:userfile>


Either way, you always have to prefix tags to define which namespace they are from
 
Back
Top