title: How can I resolve the schemaLocation attribute of an .XSD when all of my .XSD's are stored as resources?
body: <p>I am working on a project where I need to generate XML files based on nested XSD's.  e.g. ORDER has a reference to PERSON, PERSON has a reference to ADDRESS, etc.</p>
  
  <p>I am creating an `XmlReaderSettings' instance to validate the XSD's, as well as validate the XML after it is generated.</p>
  
  <p>I have added the XSD's as Resources to my assembly.  I then create an <code>XmlSchema</code> instance for each resource, from lowest to highest, and add it to the <code>XmlReaderSettings.Schemas</code> collection.</p>
  
  <p>However, this fails attempting to add a schema that references another schema.  I get an XmlSchemaException: "For element declaration, either the name or the ref attribute must be present."</p>
  
  <p>I have included sample XSD's and source code below:</p>
  
  <p><strong>ADDRESS.xsd</strong> - referenced by PERSON.xsd</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema id="ADDRESS.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:element name="ADDRESS" &gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="ADDRESS1" type="xs:string"/&gt;
          &lt;xs:element name="ADDRESS2" type="xs:string"/&gt;
          &lt;xs:element name="CITY" type="xs:string"/&gt;
          &lt;xs:element name="STATE" type="xs:string"/&gt;
          &lt;xs:element name="ZIP" type="xs:string"/&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p><strong>PERSON.xsd</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema id="PERSON.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;xs:include schemaLocation="ADDRESS.xsd"/&gt;
    &lt;xs:element name="PERSON" &gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="L_NAME" type="xs:string"/&gt;
          &lt;xs:element name="F_NAME" type="xs:string"/&gt;
          &lt;xs:element name="Addresses"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                &lt;xs:element ref="ADDRESS" minOccurs="0" maxOccurs="unbounded"/&gt;
              &lt;/xs:sequence&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><strong>Sample Code</strong> - Load and validate XSD's </p>
  
  <pre><code>using System;
  using System.IO;
  using System.Text;
  using System.Xml;
  using System.Xml.Schema;
  using SchemaTest.Properties;
  
  namespace SchemaTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              // create validation settings instance
              var xmlReaderSettings = new XmlReaderSettings
              {
                  ValidationType = ValidationType.Schema,
                  ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema |
                                    XmlSchemaValidationFlags.ProcessSchemaLocation |
                                    XmlSchemaValidationFlags.ReportValidationWarnings
              };
  
              xmlReaderSettings.ValidationEventHandler += SchemaValidationEventHandler;
  
              // added XmlResourceResolver, per the accepted answer below
              xMLReaderSettings.Schemas.XmlResolver = new XmlResourceResolver();
  
              // load schemas into validation settings instance
              LoadSchema(xmlReaderSettings, Resources.PERSON);
  
              // validate schemas
              xmlReaderSettings.Schemas.Compile();
  
              Console.WriteLine("Press any key to continue...");
              Console.ReadKey();
          }
  
          private static void LoadSchema(XmlReaderSettings xmlReaderSettings, string schemaString)
          {
              using (var schemaStream = new MemoryStream(Encoding.Default.GetBytes(schemaString)))
              {
                  var schema = XmlSchema.Read(schemaStream, null);
  
                  try
                  {
                      xmlReaderSettings.Schemas.Add(schema);
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine("EXCEPTION: {0}", ex.Message);
                  }
              }
          }
  
          private static void SchemaValidationEventHandler(object sender, ValidationEventArgs e)
          {
              Console.WriteLine("{0}: {1}", e.Severity, e.Message);
          }
      }
  }
  </code></pre>
  
  <p>I am not sure if this has anything to do with the <code>ValidationFlags</code>, but I tried removing <code>XmlSchemaValidationFlags.ProcessSchemaLocation</code> and still received the same error.  I have also tried passing <code>SchemaValidationEventHandler</code> to the <code>XmlSchema.Read</code> method (instead of null in sample code), and the schema seems to be created OK, but it still throws the exception when attemtping to add it to the <code>XmlReaderSettings.Schemas</code> collection.</p>
  
  <p><strong>EDIT - 2011.11.03</strong></p>
  
  <p>I resolved this issue (pun intended) by creating my own <code>XmlUrlResolver</code> descendent called <code>XmlResourceResolver</code>, based on the <code>XmlResolver</code> class example in accepted answer below.</p>