title: How to use namespaces to validate XML
body: <p>I'm trying to understand how to validate an XML document with multiple namespaces in it and not getting very far. As a simplified example of what I'm doing, I have a "root" element defined in one namespace like this:</p>
  
  <p><strong>File:</strong> "root.xsd"</p>
  
  <pre><code>&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:complexType name="root"/&gt;
      &lt;xs:element name="root"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:complexContent&gt;
                  &lt;xs:extension base="root"&gt;
                      &lt;xs:sequence&gt;
                          &lt;xs:element ref="allowedChild"/&gt;
                      &lt;/xs:sequence&gt;
                  &lt;/xs:extension&gt;
              &lt;/xs:complexContent&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:complexType name="allowedChild"/&gt;
      &lt;xs:element name="allowedChild" type="allowedChild"/&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Then a "child" element defined like this:</p>
  
  <p><strong>File:</strong> "child.xsd"</p>
  
  <pre><code>&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:include schemaLocation="root.xsd"/&gt;
      &lt;xs:complexType name="child"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="allowedChild"/&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
      &lt;xs:element name="child" type="child" substitutionGroup="allowedChild"/&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>And an xml that I'd like to validate that I think should look like this:</p>
  
  <pre><code>&lt;root xmlns="root.xsd"&gt;
      &lt;child xmlns="child.xsd"/&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>If I try to validate this in XmlSpy I get "Unable to locate a reference to a supported schema type (DTD, W3C Schema) within this document instance.".</p>
  
  <p>I also tried to write some C# to validate (that's my ultimate goal). My code looks like this:</p>
  
  <pre><code>static void Main(string[] args)
  {
      using (var stream = new StreamReader("Example.xml"))
      {
          XmlSchemaSet schemaSet = new XmlSchemaSet();
          schemaSet.ValidationEventHandler += MyValidationEventHandler;
          schemaSet.XmlResolver = new XmlUrlResolver();
  
          schemaSet.Add(null, "root.xsd");
  
          XmlReaderSettings settings = new XmlReaderSettings()
          {
              ValidationType = ValidationType.Schema,
              ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
              Schemas = schemaSet,
          };
  
          settings.ValidationEventHandler += MyValidationEventHandler;
  
          XmlReader reader = XmlReader.Create(stream, settings);
          while (reader.Read())
          {
          }
      }
  }
  
  static void MyValidationEventHandler(object sender, ValidationEventArgs e)
  {
  }
  </code></pre>
  
  <p>but that gives me the following validation errors:</p>
  
  <pre><code>Could not find schema information for the element 'root.xsd:root'.
  Could not find schema information for the element 'child.xsd:child'.
  </code></pre>
  
  <p>I was hoping that the XmlUrlResolver would find that xsd files (saved in the same folder), but I guess its not doing that???</p>
  
  <p>My goal is to have multiple child.xsd type files, that are produced after compile time and resolved only at run time. Is this possible?</p>