title: How can I get all the XSD validation errors from an XML file in dotnet?
body: <p>Caveat: I'm fairly new to .NET so I'm unfamiliar with a lot of libraries available.</p>
  <p>I have developed a function app using java that I'm having to port over to C#.  I am currently using the built in XSD validation library for .NET and my file <em>is</em> being successfully validated, however when testing the results against the java version there are significantly less validation errors being reported in the .NET version.</p>
  <p><strong>Root XSD file</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;xs:schema xmlns=&quot;schema_namespace&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot; targetNamespace=&quot;schema_namespace&quot; elementFormDefault=&quot;qualified&quot; attributeFormDefault=&quot;unqualified&quot;&gt;
      &lt;xs:include schemaLocation=&quot;included_xsd_1.xsd&quot;/&gt;
      &lt;xs:include schemaLocation=&quot;included_xsd_2.xsd&quot;/&gt;
      &lt;xs:include schemaLocation=&quot;included_xsd_3.xsd&quot;/&gt;
      &lt;xs:include schemaLocation=&quot;included_xsd_4.xsd&quot;/&gt;
      &lt;xs:include schemaLocation=&quot;included_xsd_5.xsd&quot;/&gt;
      &lt;xs:element name=&quot;DEMDataSet&quot;&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Root Tag&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:element name=&quot;dCustomElement&quot; type=&quot;dCustomElement&quot; id=&quot;dElementSection&quot; minOccurs=&quot;0&quot;&gt;
                      &lt;xs:annotation&gt;
                          &lt;xs:documentation&gt;Contains information for custom elements.&lt;/xs:documentation&gt;
                      &lt;/xs:annotation&gt;
                  &lt;/xs:element&gt;
                  &lt;xs:element name=&quot;Report&quot; id=&quot;ReportGroup&quot; maxOccurs=&quot;unbounded&quot;&gt;
                      &lt;xs:annotation&gt;
                          &lt;xs:documentation&gt;Container Tag to hold each instance of a record&lt;/xs:documentation&gt;
                      &lt;/xs:annotation&gt;
                      &lt;xs:complexType&gt;
                          &lt;xs:sequence&gt;
                              &lt;xs:element name=&quot;from_included_xsd_1&quot; type=&quot;type&quot; id=&quot;typeSection&quot;&gt;
                                  &lt;xs:annotation&gt;
                                      &lt;xs:documentation&gt;Some Information&lt;/xs:documentation&gt;
                                  &lt;/xs:annotation&gt;
                              &lt;/xs:element&gt;
                              &lt;xs:element name=&quot;from_included_2&quot; type=&quot;type&quot; id=&quot;typeSection&quot; minOccurs=&quot;0&quot;&gt;
                                  &lt;xs:annotation&gt;
                                      &lt;xs:documentation&gt;Some Information&lt;/xs:documentation&gt;
                                  &lt;/xs:annotation&gt;
                              &lt;/xs:element&gt;
                              
                              ...
                              
                              
                          &lt;/xs:sequence&gt;
                          &lt;xs:attribute name=&quot;timeStamp&quot; type=&quot;DateTimeType&quot; use=&quot;required&quot;/&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>My issue appears to be occuring when there is a missing element in one of my included xsd files, when this happens all subsequent errors in that xsd file are not reported (e.g. if the initial element is missing, no other validation errors are reported for that xsd file and the validation moves on to the next included xsd).</p>
  <p>My code is pretty much lifted straight from the <a href="https://docs.microsoft.com/en-us/dotnet/standard/data/xml/xml-schema-xsd-validation-with-xmlschemaset" rel="nofollow noreferrer">Microsoft documentation</a>:</p>
  <pre><code>    public XsdValidationResult ValidateXml(string filePath)
      {
          var schemas = GetSchemas();
          var settings = new XmlReaderSettings();
          settings.Schemas.Add(schemas);
          settings.ValidationType = ValidationType.Schema;
          settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
          settings.ValidationEventHandler += XsdValidationEventHandler;
  
          using var reader = XmlReader.Create(filePath, settings);
          var doc = new XmlDocument
          {
              PreserveWhitespace = true
          };
          doc.Load(reader);
          return GetValidationResult();
      }
  
      private void XsdValidationEventHandler(object sender, ValidationEventArgs e)
      {
          if (e.Severity != XmlSeverityType.Error) return;
          var sb = new StringBuilder();
          sb.Append(e.Message);
          sb.Append($&quot; Line Number: {e.Exception.LineNumber}&quot;);
          sb.Append($&quot; Position: {e.Exception.LinePosition}&quot;);
          _errors.Add(sb.ToString());
      }
  </code></pre>
  <p>The validation error I get looks something like this (before moving on to the next schema):</p>
  <blockquote>
  <p>The element 'subSchema.elementGroup' in namespace 'namespace' has
  invalid child element 'subSchema.element02' in namespace 'namespace'.
  List of possible elements expected: 'subSchema.element03' in namespace
  'namespace'.</p>
  </blockquote>
  <p>I did come across this possible explanation of what is happening in the Microsoft docs:</p>
  <blockquote>
  <p>When the new XmlReader has been closed, the original XmlReader will be
  positioned on the EndElement node of the sub-tree. Thus, if you called
  the ReadSubtree method on the start tag of the book element, after the
  sub-tree has been read and the new XmlReader has been closed, the
  original XmlReader is positioned on the end tag of the book element.</p>
  </blockquote>
  <p>but this behaviour is different from what I need :(</p>
  <p>Ultimately my question is, is there a way to continue parsing the XML file after an element has been discovered as missing (either using a different library or through the included .NET library) so as to capture all XSD validation errors?</p>