title: C# Validate xml against multiple schemas xsd
body: <p>I am looking for a way to validate my xml file against multiple xsd schemas in memory or at runtime without touching the disk filesystem.</p>
  
  <p>Let's say I have an xml file like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;addresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation='test.xsd'&gt;
  &lt;/addresses&gt;
  </code></pre>
  
  <p>then there is a schema:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;schema targetNamespace="xxx"&gt;
     &lt;!-- include and import declarations --&gt;
     &lt;include schemaLocation="another.xsd"/&gt;
     &lt;import schemaLocation="dev.xsd"/&gt;
   &lt;!--
     this .xsd does not have any schema components defined.
     removed from schema file set.    
     &lt;include schemaLocation="new.xsd"/&gt;
    --&gt;
   &lt;include schemaLocation="old.xsd"/&gt;
   &lt;include schemaLocation="test4.xsd"/&gt;
   &lt;include schemaLocation="test2.xsd"/&gt;
   &lt;include schemaLocation="test3.xsd"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>Let's say that some dumbed down diagram looks like this:</p>
  
  <pre><code>                                  test.xsd
                            |            |         |
                          test1.xsd  test2.xsd  test3.xsd
                            |     |         
                     test5.xsd   test6.xsd        
  </code></pre>
  
  <p>and so on...</p>
  
  <p>When I am using any tool for schema validation purpose and I will put all those xsd files in one folder then I can validate the xml just fine.</p>
  
  <p>I would like to achieve the same effect without using a directory on a filesystem.
  I was thinking about puting all those xsd in a zip then at runtime get them back and put them in XmlSchemaSet. The problem is that I may have some additional xsd files that are not related to the test.xml.</p>
  
  <p>Is there any way to validate the xml with mutiple xsd in memory? I was thinking about using:</p>
  
  <pre><code>           foreach (String singleSchema in schema.listOfSchemasReferenced)
              {
                  using (StringReader stringReader = new StringReader(singleSchema))
                  using (XmlTextReader xmlReader = new XmlTextReader(stringReader))
                  {
                      var includeSchema = XmlSchema.Read(xmlReader, null);
  
                      var include = new XmlSchemaInclude();
                      include.Schema = includeSchema;
                      if (mainSchema.TargetNamespace != includeSchema.TargetNamespace)
                          continue;
                      mainSchema.Includes.Add(include);
                  }
              }
  </code></pre>
  
  <p>I am getting errors then  I am trying compile schemaset because the listOfSchemasReferenced may have a lot of unrealated xsd placed in the same folder.
  While i was looking through the web, i've seen that XmlResolver may be usefull but I am not sure how to use in the context of validation multiple xsd in a context of files placed in a .zip and memory.
  If you have any ideas how to solve this problem or you have some examples that I could read that would be great. Any help appreciated.</p>