1

Note: I've looked at this question but still was not able to understand how to correctly apply the information there.

I am pretty much still a newbie to XML, but have created an XML file that starts like this:

<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <Model>
    <Name>My Configuration</Name>
    <Version>0.0.1</Version>
  </Model>
</Configuration>

The XSD looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema id="Configuration" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="Configuration">
    <!-- and so on -->
  </xs:element>
</xs:schema>

Now in my C# program I can validate it against a known XSD schema using the XDocument.Validate() method, and I can desearialize it using an XmlSerializer and classes generated from my XSD (using XSD.exe) no problem.

However I am attempting to have the XSD referenced inline in the XML, adding the xsi:schemaLocation attribute to the root <Configuration> element, like so:

<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.w3.org file:///C:/Users/Me/Data/ConfigSchema.xsd">

I have the C# code building OK (using the XmlReader.Create() method), but it always tells me the following error message for all elements:

Could not find schema information for the element ''.

What am I doing wrong? Do I need to change something else in the XML or XSD?

UPDATE: I can make this work by adding an XSD targetNamepsace equal to the XML xmlns and xsi:schemaLocation urn. But then normal deserialization fails on the first element (i.e. the Configuration element containing this namespace/schema mess)

Toby
  • 9,696
  • 16
  • 68
  • 132
  • 1
    Your `Configuration` element is not in a namespace, despite all of the namespace related machinery you've tried to include. See the duplicate link for how to properly use `xsi:schemaLocation` and `xsi:noNamespaceSchemaLocation`. Also, don't use a `http://www.w3.org` for your own XSDs -- use a URL that you control. (`xsi:schemaLocation="http://www.w3.org file:///C:/Users/Me/Data/ConfigSchema.xsd"` is not what you want. Again, see duplicate link for the proper pattern to follow.) – kjhughes Aug 08 '17 at 18:25
  • @kjhughes Thanks. What if i don't *have* a URL I control? This XML is never going to be used online it's more for application formatted data storage (yes, I know there are other methods that may be better suited to this, but XML is a requirement). – Toby Aug 09 '17 at 09:19
  • 1
    You could use `http://www.example.com/whatever`. – kjhughes Aug 09 '17 at 14:32
  • @kjhughes Is there any difference between `example.com` and `w3.org`? I don't have control of either and both have (or could have) non-XML content at a given address, right? – Toby Aug 09 '17 at 14:39
  • 1
    Nothing authorizes such use of a `w3.org`, but [**RFC2606**](https://tools.ietf.org/html/rfc2606) does authorize use of `example.com` for testing. Note also that [**namespace URIs do not have to be retrievable**](https://stackoverflow.com/a/27614076/290085). – kjhughes Aug 09 '17 at 14:53
  • @kjhughes Awesome, thank you Kenneth. – Toby Aug 09 '17 at 15:06

0 Answers0