title: Getting started with XSD validation with .NET
body: <p>Here is my first attempt at validating XML with XSD.</p>
  
  <p>The XML file to be validated:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;config xmlns="Schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="config.xsd"&gt;
    &lt;levelVariant&gt;
      &lt;filePath&gt;SampleVariant&lt;/filePath&gt;
    &lt;/levelVariant&gt;
    &lt;levelVariant&gt;
      &lt;filePath&gt;LegendaryMode&lt;/filePath&gt;
    &lt;/levelVariant&gt;
    &lt;levelVariant&gt;
      &lt;filePath&gt;AmazingMode&lt;/filePath&gt;
    &lt;/levelVariant&gt;
  &lt;/config&gt;
  </code></pre>
  
  <p>The XSD, located in "Schemas/config.xsd" relative to the XML file to be validated:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"&gt;
    &lt;xs:element name="config"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="levelVariant"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                &lt;xs:element name="filePath" type="xs:anyURI"&gt;
                &lt;/xs:element&gt;
              &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Right now, I just want to validate the XML file precisely as it appears currently. Once I understand this better, I'll expand more. Do I really need so many lines for something as simple as the XML file as it currently exists?</p>
  
  <p>The validation code in C#:</p>
  
  <pre><code>        public void SetURI(string uri)
          {
              XElement toValidate = XElement.Load(Path.Combine(PATH_TO_DATA_DIR, uri) + ".xml");
  
  // begin confusion
  
         // exception here
         string schemaURI = toValidate.Attributes("xmlns").First().ToString() 
                                + toValidate.Attributes("xsi:noNamespaceSchemaLocation").First().ToString();
          XmlSchemaSet schemas = new XmlSchemaSet();
          schemas.Add(null, schemaURI);
  
          XDocument toValidateDoc = new XDocument(toValidate);
          toValidateDoc.Validate(schemas, null);
  // end confusion
  
              root = toValidate;
          }
  </code></pre>
  
  <p>Running the above code gives this exception:</p>
  
  <pre><code>The ':' character, hexadecimal value 0x3A, cannot be included in a name.
  </code></pre>
  
  <p>Any illumination would be appreciated.</p>