title: How to include multiple XML files in a single XML file for deserialization by XmlSerializer in .NET
body: <p>is it possible to use the <code>XmlSerializer</code> in .NET to load an XML file which includes other XML files? And how? </p>
  
  <p>This, in order to share XML state easily in two "parent" XML files, e.g. AB and BC in below.</p>
  
  <p>Example:</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Xml.Serialization;
  
  namespace XmlSerializerMultipleFilesTest
  {
      [Serializable]
      public class A
      {
          public int Value
          { get; set; }
      }
  
      [Serializable]
      public class B
      {
          public double Value
          { get; set; }
      }
  
      [Serializable]
      public class C
      {
          public string Value
          { get; set; }
      }
  
      [Serializable]
      public class AB
      {
          public A A
          { get; set; }
          public B B
          { get; set; }
      }
  
      [Serializable]
      public class BC
      {
          public B B
          { get; set; }
          public C C
          { get; set; }
      }
  
      class Program
      {
          public static void Serialize&lt;T&gt;(T data, string filePath)
          {
              using (var writer = new StreamWriter(filePath))
              {
                  var xmlSerializer = new XmlSerializer(typeof(T));
                  xmlSerializer.Serialize(writer, data);
              }
          }
          public static T Deserialize&lt;T&gt;(string filePath)
          {
              using (var reader = new StreamReader(filePath))
              {
                  var xmlSerializer = new XmlSerializer(typeof(T));
                  return (T)xmlSerializer.Deserialize(reader);
              }
          }
  
          static void Main(string[] args)
          {
              const string fileNameA = @"A.xml";
              const string fileNameB = @"B.xml";
              const string fileNameC = @"C.xml";
  
              const string fileNameAB = @"AB.xml";
              const string fileNameBC = @"BC.xml";
  
              var a = new A(){ Value = 42 };
              var b = new B(){ Value = Math.PI };
              var c = new C(){ Value = "Something rotten" };
  
              Serialize(a, fileNameA);
              Serialize(b, fileNameB);
              Serialize(c, fileNameC);
  
              // How can AB and BC be deserialized from single 
              // files which include two of the A, B or C files.
              // Using ideally something like:
              var ab = Deserialize&lt;AB&gt;(fileNameAB);
              var bc = Deserialize&lt;BC&gt;(fileNameBC);
              // That is, so that A, B, C xml file 
              // contents are shared across these two
          }
      }
  }
  </code></pre>
  
  <p>Thus, the A, B, C files contain the following:</p>
  
  <p><code>A.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;Value&gt;42&lt;/Value&gt;
  &lt;/A&gt;
  </code></pre>
  
  <p><code>B.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;B xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;Value&gt;3.1415926535897931&lt;/Value&gt;
  &lt;/B&gt;
  </code></pre>
  
  <p><code>C.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;C xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;Value&gt;Something rotten&lt;/Value&gt;
  &lt;/C&gt;
  </code></pre>
  
  <p>And then the "parent" XML files would contain a XML include file of some sort (I have not been able to find anything like this), such as:</p>
  
  <p><code>AB.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;AB xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;A include="A.xml"/&gt;
    &lt;B include="B.xml"/&gt;
  &lt;/AB&gt;
  </code></pre>
  
  <p><code>BC.xml</code>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;BC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;B include="B.xml"/&gt;
    &lt;C include="C.xml"/&gt;
  &lt;/BC&gt;
  </code></pre>
  
  <p>Of course, I guess this can be solved by implementing <code>IXmlSerializer</code> for AB and BC, but I was hoping there was an easier solution or a generic solution with which classes themselves only need the <code>[Serializable]</code> attribute and nothing else. That is, the split into multiple files is XML only and handled by <code>XmlSerializer</code> itself or a custom generic serializer on top of this.</p>
  
  <p>I know this should be somewhat possible with app.config (as in <a href="https://stackoverflow.com/questions/480538/use-xml-includes-or-config-references-in-app-config-to-include-other-config-files">Use XML includes or config references in app.config to include other config files&#39; settings</a>), but I would prefer a solution based on XmlSerializer.</p>
  
  <p>Thanks.</p>