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)