title: Capture Schema Information when validating XDocument
body: <p>This is similar to this question <a href="https://stackoverflow.com/questions/7468913/c-sharp-get-schema-information-when-validating-xml">C# Get schema information when validating xml</a></p>
  
  <p>However, I am working with an XDocument for LINQ purposes.</p>
  
  <p>I am reading/parsing a set of CSV files and converting to XML, then validating the XML against an XSD schema.</p>
  
  <p>I would like to capture specific errors related to the element values, generate a more user friendly message, and give them back to the user so the input data can be corrected.  One of the items I would like to include in the output data is some schema information (such as the range of acceptable values for a numeric type).</p>
  
  <p>In my current approach (which I am open to changing), I am able to capture everything I need except for the schema information.</p>
  
  <p>I've tried accessing the <code>SourceSchemaObject</code> in the <code>ValidationEventArgs</code> argument of the Validation event handler, but that is always null.  I've also tried the <code>GetSchemaInfo</code> of the XElement and that appears to be null also.</p>
  
  <p>I am using RegEx to identify the specific validation errors i want to capture, and grabbing data from the XElement via the sender argument of the validation event handler.  I've thought of converting the schema to an XDocument and grabbing what I need via LINQ, but it seems to me that there should be a better option</p>
  
  <p>Here's my current Validate Method:</p>
  
  <pre><code>private List&lt;String&gt; this.validationWarnings;
  private XDocument xDoc;
  private XmlSchemaSet schemas = new XmlSchemaSet();
  
  public List&lt;String&gt; Validate()
  {
      this.validationWarnings = new List&lt;String&gt;();
  
      // the schema is read elsewhere and added to the schema set
      this.xDoc.Validate(this.schemas, new ValidationEventHandler(ValidationCallBack), true);
  
      return validationWarnings
  }
  </code></pre>
  
  <p>And here's my callback method:</p>
  
  <pre><code>private void ValidationCallBack(object sender, ValidationEventArgs args)
  {           
      var element = sender as XElement;
  
      if (element != null)
      {
  
          // this is a just a placeholder method where I will be able to extract the 
          //  schema information and put together a user friendly message for specific 
          //  validation errors    
          var message = FieldValidationMessage(element, args);
  
          // if message is null, then the error is not one one I wish to capture for 
          //  the user and is related to an invalid XML structure (such as missing 
          //  elements or incorrect order).  Therefore throw an exception
          if (message == null)
              throw new InvalidXmlFileStructureException(args.Message, args.Exception);
          else
              validationWarnings.Add(message);
  
      }
  }
  </code></pre>
  
  <p>The <code>var message = FieldValidationMessage(element, args);</code> line in my callback method is just a placeholder and does not exist yet   The intention of this method is to do 3 things:</p>
  
  <ol>
  <li><p>Identify specific validation errors by using RegEx on <code>args.Message</code> (this already works, I have tested patterns that I plan on using)</p></li>
  <li><p>Grab attribute values from the XDocument related to the specific XElement that is causing the error (such as the row and column number in the original CSV)</p></li>
  <li><p>Grab the schema information if it is available so field types and restrictions can be added to the output message.</p></li>
  </ol>