title: Validation of optional attributes against xsd-schema in powershell
body: <p>I need to validate xml against xsd by powershell script with warnings about optional attributes, etc. 
  For example, My xsd:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  
  
    &lt;xs:complexType name="LocalizedValue"&gt;
      &lt;xs:attribute name="Lang" type="xs:string" use="required"&gt;
        &lt;xs:annotation&gt;
          &lt;xs:documentation&gt;Language Code&lt;/xs:documentation&gt;
        &lt;/xs:annotation&gt;
      &lt;/xs:attribute&gt;
      &lt;xs:attribute name="Name" type="xs:string" use="required"&gt;
        &lt;xs:annotation&gt;
          &lt;xs:documentation&gt;District Typr&lt;/xs:documentation&gt;
        &lt;/xs:annotation&gt;
      &lt;/xs:attribute&gt;
      &lt;xs:attribute name="ShortName" type="xs:string" use="optional"&gt;
        &lt;xs:annotation&gt;
          &lt;xs:documentation&gt;Аbbreviation&lt;/xs:documentation&gt;
        &lt;/xs:annotation&gt;
      &lt;/xs:attribute&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:complexType name="DistrictType"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="Localizations" minOccurs="1" maxOccurs="1"&gt;
          &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
              &lt;xs:element name="Localization" type="LocalizedValue"  minOccurs="1" maxOccurs="unbounded"&gt;
                &lt;xs:annotation&gt;
                  &lt;xs:documentation&gt;Local attributes&lt;/xs:documentation&gt;
                &lt;/xs:annotation&gt;
              &lt;/xs:element&gt;
            &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
        &lt;xs:element name="Branches"&gt;
          &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
              &lt;xs:element name="Branch" minOccurs="0" maxOccurs="unbounded"&gt;
                &lt;xs:complexType&gt;
                  &lt;xs:attribute name="Code" type="xs:int" use="required"&gt;
                    &lt;xs:annotation&gt;
                      &lt;xs:documentation&gt;Branch Code&lt;/xs:documentation&gt;
                    &lt;/xs:annotation&gt;
                  &lt;/xs:attribute&gt;
                &lt;/xs:complexType&gt;
              &lt;/xs:element&gt;
            &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
      &lt;/xs:sequence&gt;
      &lt;xs:attribute name="Code" type="xs:int" use="required"&gt;
        &lt;xs:annotation&gt;
          &lt;xs:documentation&gt;Dictionary code&lt;/xs:documentation&gt;
        &lt;/xs:annotation&gt;
      &lt;/xs:attribute&gt;
      &lt;xs:attribute name="GroupCode" type="xs:int" use="optional"&gt;
        &lt;xs:annotation&gt;
          &lt;xs:documentation&gt;Another Code&lt;/xs:documentation&gt;
        &lt;/xs:annotation&gt;
      &lt;/xs:attribute&gt;
      &lt;xs:attribute name="IsDeleted" type="xs:boolean" use="optional" default="false"&gt;
        &lt;xs:annotation&gt;
          &lt;xs:documentation&gt;
           documentation
          &lt;/xs:documentation&gt;
        &lt;/xs:annotation&gt;
      &lt;/xs:attribute&gt;
    &lt;/xs:complexType&gt;
  
    &lt;xs:element name="DistrictType" type="DistrictType"&gt;
      &lt;xs:annotation&gt;
        &lt;xs:documentation&gt;documentation&lt;/xs:documentation&gt;
      &lt;/xs:annotation&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>And xml:</p>
  
  <pre><code>&lt;DistrictType Code="1" IsDeleted="false" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./../Schema/flowGeoClassifier.DistrictType.xsd"&gt;
    &lt;Localizations&gt;
      &lt;Localization Lang="ru" Name="район" ShortName="р-н" /&gt;
    &lt;/Localizations&gt;
    &lt;Branches&gt;
      &lt;Branch Code="1" /&gt;
    &lt;/Branches&gt;
    &lt;Countries&gt;
      &lt;Country Code="1" /&gt;
      &lt;Country Code="483647" /&gt;
      &lt;Country Code="2147483647" /&gt;
    &lt;/Countries&gt;
  &lt;/DistrictType&gt;
  </code></pre>
  
  <p>I need to get some message like that:</p>
  
  <p><em>Warning: Optional attribute "GroupCode" is missing.</em></p>
  
  <p>I use a powershell script to validate:</p>
  
  <pre><code>$XmlFile = Get-Item($xmlFileName)
      # Perform the XSD Validation
      $readerSettings = New-Object -TypeName System.Xml.XmlReaderSettings
      $readerSettings.Schemas.Add($compiledSchema)
      $readerSettings.ValidationType = [System.Xml.ValidationType]::Schema
      $readerSettings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessSchemaLocation
      $readerSettings.add_ValidationEventHandler(
      {
          # Triggered each time an error is found in the XML file
          Write-Host $("ERROR line $($_.exception.LineNumber) position $($_.exception.LinePosition) in '$xmlFileName': " + $_.Message) -ForegroundColor Red
          $script:errorCount++
      });
      $reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $readerSettings)
      while ($reader.Read()) { }
      $reader.Close()
  </code></pre>
  
  <p>Has ValidationEventHandler validate optional attributes by standart methodes?</p>