title: How do I create an XmlNode from a call to XmlSerializer.Serialize?
body: <p>I am using a class library which represents some of its configuration in .xml. The configuration is read in using the <code>XmlSerializer</code>. Fortunately, the classes which represent the .xml use the <code>XmlAnyElement</code> attribute at which allows me to extend the configuration data for my own purposes without modifying the original class library.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Config&gt;
    &lt;data&gt;This is some data&lt;/data&gt;
    &lt;MyConfig&gt;
      &lt;data&gt;This is my data&lt;/data&gt;
    &lt;/MyConfig&gt;
  &lt;/Config&gt;
  </code></pre>
  
  <p>This works well for deserialization. I am able to allow the class library to deserialize the .xml as normal and the I can use my own <code>XmlSerializer</code> instances with a <code>XmlNodeReader</code> against the internal <code>XmlNode</code>.</p>
  
  <pre><code>public class Config
  {
      [XmlElement]
      public string data;
  
      [XmlAnyElement]
      public XmlNode element;
  }
  
  public class MyConfig
  {
      [XmlElement] 
      public string data;
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          using (Stream fs = new FileStream(@"c:\temp\xmltest.xml", FileMode.Open))
          {
              XmlSerializer xser1 = new XmlSerializer(typeof(Config));
              Config config = (Config)xser1.Deserialize(fs);
  
              if (config.element != null)
              {
                  XmlSerializer xser2 = new XmlSerializer(typeof(MyConfig));
                  MyConfig myConfig = (MyConfig)xser2.Deserialize(new XmlNodeReader(config.element));
              }
          }
      }
  </code></pre>
  
  <p>I need to create a utility which will allow the user to generate a new configuration file that includes both the class library configuration as well my own configuration, so new objects will be created which were not read from the .xml file. The question is how can I serialize the data back into .xml? </p>
  
  <p>I realize that I have to initially call <code>XmlSerializer.Serialize</code> on my data before calling the same method on the class library configuration. However, this requires that my data is represented by an <code>XmlNode</code> after calling <code>Serialize</code>. What is the best way to serialize an object into an <code>XmlNode</code> using the <code>XmlSerializer</code>?</p>
  
  <p>Thanks,</p>
  
  <p>-kevin</p>
  
  <p>btw-- It looks like an <code>XmlNodeWriter</code> class written by Chris Lovett was available at one time from Microsoft, but the links are now broken. Does anyone know of an alternative location to get this class?</p>