title: XML verification with XSD: attribute is not declared. But it is - see example
body: <p>I'm having a problem validating XML against schema. Simplified code and examples:</p>
  
  <p>Verification code:</p>
  
  <pre><code>    public static void ValidateXmlAgainstSchema(StreamReader xml, XmlSchema xmlSchema)
      {
          var settings = new XmlReaderSettings { IgnoreWhitespace = true, IgnoreComments = true };
          settings.Schemas.Add(xmlSchema);
  
          settings.ValidationType = ValidationType.Schema;
          settings.ValidationEventHandler += (obj, args) =&gt; { if (args.Exception != null) throw args.Exception; };
  
          using (var reader = XmlReader.Create(xml, settings))
          using (XmlReader validatingReader = XmlReader.Create(reader, settings))
          {
              while (validatingReader.Read()){}
          }
      }
  </code></pre>
  
  <p>Schema:</p>
  
  <pre><code>  &lt;?xml version="1.0"?&gt;
     &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
             targetNamespace="http://foo.com/"
             xmlns="http://foo.com/"&gt;
  
    &lt;xs:simpleType name="myBool"&gt;
      &lt;xs:restriction base="xs:string"&gt;
        &lt;xs:enumeration value="true"/&gt;
        &lt;xs:enumeration value="false"/&gt;
        &lt;xs:enumeration value="file_not_found"/&gt;
      &lt;/xs:restriction&gt;
    &lt;/xs:simpleType&gt;
  
    &lt;xs:complexType name="dataType"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="id" type="xs:string" minOccurs="1" maxOccurs="1" /&gt;
        &lt;xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="1" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:element name="foo"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="data" type="dataType" minOccurs="0" maxOccurs="unbounded" /&gt;
        &lt;/xs:sequence&gt;
        &lt;xs:attribute name="myBool" type="myBool" use="optional" /&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>XML:</p>
  
  <p>1.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;foo xmlns="http://foo.com/" myBool="true"&gt;
    &lt;data&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;name&gt;abc&lt;/name&gt;
    &lt;/data&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>This example throws an exception:</p>
  
  <p>System.Xml.Schema.XmlSchemaValidationException: The element 'foo' in namespace '<a href="http://foo.com/" rel="nofollow">http://foo.com/</a>' has invalid child element 'data'
  in namespace '<a href="http://foo.com/" rel="nofollow">http://foo.com/</a>'. List of possible elements expected: 'data'.</p>
  
  <p>My understanding is that if the namespace is defined for an element, all child elements will have the same namespace, unless defined otherwise. It doesn't work though. I can make it validate by adding elementFormDefault="qualified" to the schema, which makes all elements default to targetNamespace. Is that a good way of doing it?</p>
  
  <p>2.</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;a:foo xmlns:a="http://foo.com/" a:myBool="true"&gt;
    &lt;a:data&gt;
      &lt;a:id&gt;1&lt;/a:id&gt;
      &lt;a:name&gt;abc&lt;/a:name&gt;
    &lt;/a:data&gt;
  &lt;/a:foo&gt;
  </code></pre>
  
  <p>This example fails with the message:</p>
  
  <p>The '<a href="http://foo.com/:myBool" rel="nofollow">http://foo.com/:myBool</a>' attribute is not declared.</p>
  
  <p>Every element and attribute has an explicit namespace, so the xml should be valid. Even the error message suggest parser is looking for the attribute I expect it to, but fails to find it. I can make it validate by changing a:myBool to myBool. Why doesn't it work in the first form and works in the other?</p>