title: Create and Validate XML against a schema in C# using XML Namespaces
body: <p>I'm trying to figure out how to validate XML against a schema in C#. If the XML is simple and uses no namespace elements everything seems to go great. But, if it uses XML namespaces, I start to run into problems.</p>
  
  <p>The XML I want to produce is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;SlideDeck xmlns:xy="http://something.com" xy:type="SlideDefinitions"&gt;
    &lt;Slide&gt;...&lt;/Slide&gt;
    &lt;Slide&gt;...&lt;/Slide&gt;
    ...
  &lt;/SlideDeck&gt;
  </code></pre>
  
  <p>I can produce this in C# with something equivalent to:</p>
  
  <pre><code>XmlDocument xDoc = new XmlDocument();
  XmlElement xSlideDeck = xDoc.CreateElement("SlideDeck");
  xDoc.AppendChild(xSlideDeck);
  
  xSlideDeck.SetAttribute("xmlns:xy", "http://something.com");
  xSlideDeck.SetAttribute("type", "http://something.com", "SlideDefinitions");
  </code></pre>
  
  <p>And I can attempt to validate the produced XML document with this:</p>
  
  <pre><code>xDoc.Schemas.Add("", "Schema.xsd");
  xDoc.Validate(Handler);
  </code></pre>
  
  <p>The Schema.xsd file to validate against is fairly simply right now:</p>
  
  <pre><code>&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:element name="SlideDeck"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:choice maxOccurs="unbounded"&gt;
                      &lt;xs:element ref="Slide"/&gt;
                  &lt;/xs:choice&gt;
              &lt;/xs:sequence&gt;
              &lt;xs:attribute name="type" type="xs:string" use="required" /&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      ... Slide Defined Here ...
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>When running the validation, I get the following errors:</p>
  
  <pre><code>The 'http://something.com:type' attribute is not declared.
  The required attribute 'type' is missing.
  </code></pre>
  
  <p>I've also tried adding an additional schema to the XmlDocument that references <a href="http://something.com" rel="nofollow">http://something.com</a> with the same schema file (hoping that it would pull that in for the 'xy' namespace). I've also tried adding just a schema to the proper uri (leaving out the schema with no uri), but depending on other settings, nothing is validated. I've tried making the root 'SlideDeck' element belong to the <a href="http://something.com" rel="nofollow">http://something.com</a> namespace as well.</p>
  
  <p>Any idea what might be happening here? I can't change the format of the XML document coming out much at all. I can make the 'SlideDeck' elements prefixed with 'xy', along with all the other elements, but I'd prefer not to have to modify all the code to additionally add the uri to the namespace and prefix to make that happen. The big sticking points are the xmlns:xy definition and the xy:type attribute which alert the recipient to what to expect. Other changes are reasonable. I can also control the XSD the program validates against as well if getting it to create and validate would best be accomplished by changes there.</p>