<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>

[{'answer_id': 46378, 'author': 'Ed Schwehm', 'author_id': 1206, 'author_profile': 'https://Stackoverflow.com/users/1206', 'pm_score': 5, 'selected': True, 'text': '<p>So you need to have your class contain custom configuration information, then serialize that class to XML, then make that serialized XML into an XML node: is that right?</p>\n\n<p>Could you just take the string created by the XMLSerializer and wrap that in it\'s own XML tags?</p>\n\n<pre><code>XmlSerializer xs = new XmlSerializer(typeof(MyConfig));\nStringWriter xout = new StringWriter();\nxs.Serialize(xout, myConfig);\nXmlDocument x = new XmlDocument();\nx.LoadXml("&lt;myConfig&gt;" + xout.ToString() + "&lt;/myConfig&gt;");\n</code></pre>\n\n<p>Now x is an XmlDocument containing one element, "&lt;myconfig&gt;", which has your serialized custom configuration in it.</p>\n\n<p>Is that at all what you\'re looking for? </p>\n'}, {'answer_id': 46401, 'author': 'Kevin', 'author_id': 4599, 'author_profile': 'https://Stackoverflow.com/users/4599', 'pm_score': 0, 'selected': False, 'text': '<p>One solution is to serialize the inner object to a string and then load the string into a XmlDocument where you can find the XmlNode representing your data and attach it to the outer object. </p>\n\n<pre><code>XmlSerializer xser1 = new XmlSerializer(typeof(Config));\nXmlSerializer xser2 = new XmlSerializer(typeof(MyConfig));\n\nMyConfig myConfig = new MyConfig();\nmyConfig.data = "My special data";\n\nStringBuilder sb = new StringBuilder();\nStringWriter sw = new StringWriter(sb);\nXmlWriter xw = new XmlTextWriter(sw);\nxser2.Serialize(xw, myConfig);\n\nXmlDocument doc = new XmlDocument();\ndoc.LoadXml(sb.ToString());\n\nConfig config = new Config();\nconfig.data = "some new info";\nconfig.element = doc.LastChild;\n\nxser1.Serialize(fs, config);\n</code></pre>\n\n<p>However, this solution is cumbersome and I would hope there is a better way, but it resolves my problem for now.</p>\n\n<p>Now if I could just find the mythical XmlNodeWriter referred to on several blogs!</p>\n'}, {'answer_id': 46457, 'author': 'Ed Schwehm', 'author_id': 1206, 'author_profile': 'https://Stackoverflow.com/users/1206', 'pm_score': 0, 'selected': False, 'text': '<p>At least one resource points to this as an alternative to XmlNodeWriter: <a href="http://msdn.microsoft.com/en-us/library/5x8bxy86.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/5x8bxy86.aspx</a>. Otherwise, you could write MS using that form they have on the new MSDN Code Library replacement for GotDotNet looking for XmlNodeWriter.</p>\n'}, {'answer_id': 307707, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>It took a bit of work, but the XPathNavigator route does work... just remember to call .Close on the XmlWriter, .Flush() doesn't do anything:</p>\n\n<pre><code>//DataContractSerializer serializer = new DataContractSerializer(typeof(foo));\nXmlSerializer serializer = new XmlSerializer(typeof(foo));\n\nXmlDocument doc = new XmlDocument();\nXPathNavigator nav = doc.CreateNavigator();\nXmlWriter writer = nav.AppendChild();\nwriter.WriteStartDocument();\n//serializer.WriteObject(writer, new foo { bar = 42 });\nserializer.Serialize(writer, new foo { bar = 42 });\nwriter.WriteEndDocument();\nwriter.Flush();\nwriter.Close();\n\nConsole.WriteLine(doc.OuterXml);\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/46282', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4599/']