title: Partial XML file validation using XSD
body: <p>I am trying to use the XDocument class and XmlSchemaSet class to validate an XMl file.</p>
  
  <p>The XML file already exists but I want to add in just a single element consisting of a couple other elements and I only want to validate this node.</p>
  
  <p>Here is an example of the XML file. The piece I would like to validate is the <code>TestConfiguration</code> node:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;Root&gt;
    &lt;AppType&gt;Test App&lt;/AppType&gt;
    &lt;LabelMap&gt;
      &lt;Label0&gt;
          &lt;Title&gt;Tests&lt;/Title&gt;
          &lt;Indexes&gt;1,2,3&lt;/Indexes&gt;
      &lt;/Label0&gt;
    &lt;/LabelMap&gt;
  
  &lt;TestConfiguration&gt;
      &lt;CalculateNumbers&gt;true&lt;/CalculateNumbers&gt;
      &lt;RoundToDecimalPoint&gt;3&lt;/RoundToDecimalPoint&gt;
  &lt;/TestConfiguration&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Here is my xsd so far:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema id="TestConfiguration"
             targetNamespace="MyApp_ConfigurationFiles" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  
    &lt;xs:element name="TestConfiguration"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="CalculateNumbers" type="xs:boolean" minOccurs="1" maxOccurs="1"/&gt;
          &lt;xs:element name="RoundToDecimalPoint" type="xs:int" minOccurs="1" maxOccurs="1"/&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>Here is the code I use to validate it:</p>
  
  <pre><code>private bool ValidateXML(string xmlFile, string xsdFile)
  {
      string xsdFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? string.Empty, xsdFile);
  
      Logger.Info("Validating XML file against XSD schema file.");
      Logger.Info("XML: " + xmlFile);
      Logger.Info("XSD: " + xsdFilePath);
  
      try
      {
          XDocument xsdDocument = XDocument.Load(xsdFilePath);
          XmlSchemaSet schemaSet = new XmlSchemaSet();
          schemaSet.Add(XmlSchema.Read(new StringReader(xsdDocument.ToString()), this.XmlValidationEventHandler));
          XDocument xmlDocument = XDocument.Load(xmlFile);
          xmlDocument.Validate(schemaSet, this.XmlValidationEventHandler);
      }
      catch (Exception e)
      {
          Logger.Info("Error parsing XML file: " + xmlFile);
          throw new Exception(e.Message);
      }
  
      Logger.Info("XML validated against XSD.");
      return true;
  }
  </code></pre>
  
  <p>Even validating the full XML file, the validation will pass successfully causing me to run into problems when I try to load the XML file into the generated class file created by xsd2code, the error: <code>&lt;Root xmlns=''&gt; was not expected.</code>.</p>
  
  <p>How can I validate just the <code>TestConfiguration</code> piece?</p>
  
  <p>Thanks</p>