title: How to validate XML file against HR-XML using C#
body: <p>I'm looking for some advice and guidance for validating HR-XML.</p>
  
  <p>I've written come code to produce an XML file that "<strong><em>should</em></strong>" be correctly formatted as HR-XML but I want to validate it using code before writing it to disk.</p>
  
  <p>Below is a code sample of my validation method and the validation error event handler</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Validate the populated XML
  /// &lt;/summary&gt;
  /// &lt;remarks&gt;
  /// The schema folder needs to be "HR-XML-3_0" which contains the "org_hr-xml" and "org_openapplications_platform" folders
  /// &lt;/remarks&gt;
  /// &lt;param name="schemaPath"&gt;The root path for the HR-XML XSD files for the xml to be validated against&lt;/param&gt;
  /// &lt;returns&gt;true if the xml is valid, else false&lt;/returns&gt;
  public bool Validate(string schemaPath)
  {
      try
      {
          // Initalise the valid flag
          this.m_FormatValid = false;
          this.m_ValidationErrors.Clear();
  
          // Check if there is anything to output
          if (this.m_Root.HasElements == true)
          {
              // Validate that the root node has been populated correctly
              XDocument doc = new XDocument(m_Root);
  
              XmlSchemaSet schemas = new XmlSchemaSet();
  
              // Add the schemas in the specified folder to the schema set
              schemas.Add(XmlSchema.Read(new StreamReader(Path.Combine(schemaPath, @"org_hr-xml\3_0\Developer\BODs\RespondHRMasterData.xsd")), null));
              schemas.Add(XmlSchema.Read(new StreamReader(Path.Combine(schemaPath, @"org_openapplications_platform\1_1\Common\OAGi\Components\Meta.xsd")), null));
  
              // Set the valid flag to true prior to validation and let the event handler set it to false if it's not valid
              this.m_FormatValid = true;
  
              doc.Validate(schemas, HRXML_Validation_Error);
          }
          else
          {
              Log.WriteLine(Category.Info, "No HR-XML data to validate");
          }
      }
      catch (Exception ex)
      {
          this.m_FormatValid = false;
          Log.WriteLine(Category.Warning, "An error was detected whilst validating HR-XML data", ex);
      }
  
      return this.m_FormatValid;
  }
  
  
  
  /// &lt;summary&gt;
  /// Event handler for XML validation errors
  /// &lt;/summary&gt;
  void HRXML_Validation_Error(Object source, ValidationEventArgs args)
  {
      // There is no need to worry about the severity of the validation as they should always be errors
      // The warning appears only to be triggered is no schema has been specified which shouyldn't be the case here
  
      // Output the message to the validation list
      this.m_ValidationErrors.Add( args.Message );
  
      //Set the Valid flag to false
      m_FormatValid = false;
  }
  </code></pre>
  
  <p>I added the BOD for responding to HRMasrterData requests into the schema set but this generated an exception because of the imported schema referenced in the <strong>RespondHRMasterData.xsd</strong> file. The error was
  <strong><em>Undefined complexType 'http://www.openapplications.org/oagis/9:BusinessObjectDocumentType' is used as a base for complex type extension.</em></strong></p>
  
  <p>Adding the second file to the schema set resolved the first exception and gave this one.
  <strong><em>Type 'http://www.openapplications.org/oagis/9:NormalizedStringType' is not declared, or is not a simple type.</em></strong></p>
  
  <p>What I don't what to do is add all the HR-XML schema files (unless I really have to) before I get to and "Actual" errors in the created file.</p>
  
  <p><strong>Am I on the right track or is there a better way of doing this?</strong></p>
  
  <p>Thanks.</p>