Jump to content
Xtreme .Net Talk

Recommended Posts

  • Leaders
Posted

So I've 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:

 

<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:

 

<?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:

 

I've read and reread all the tutorials I could find on XML schemas, and I just don't get it.

 

:confused:

 

Thanks for reading.

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...