protected static XmlNode SaveConfiguration(Type instanceType, object instance,string memberToOverride, Type[] types) { XmlSerializer ser; if ( types != null ) { ser = new XmlSerializer(instanceType, GetXmlOverrides(instance.GetType(),memberToOverride,types)); } else { ser = new XmlSerializer(instanceType); } // Serialize object to xml StringWriter sw = new StringWriter( System.Globalization.CultureInfo.CurrentUICulture ); SkipSerializerNamespacesWriter writer = new SkipSerializerNamespacesWriter(sw); ser.Serialize(writer, instance); writer.Flush(); // Return as a XmlNode XmlDocument doc = new XmlDocument(); doc.LoadXml( sw.ToString() ); return doc.DocumentElement; }
/// <summary> /// Writes the configuration node. /// </summary> /// <param name="section"> The section handler name.</param> /// <param name="node"> The XmlNode data.</param> /// <param name="configFile"> The configuration file.</param> /// <param name="supportsNamespaces"> Set to true if it supports namespaces.</param> internal static void WriteConfigNode(string section, XmlNode node, string configFile, bool supportsNamespaces) { XmlDocument document = new XmlDocument(); if ( File.Exists(configFile) ) { FileStream readFile = new FileStream(configFile, FileMode.Open,FileAccess.Read); document.Load(readFile); XmlNode sectionNode = document.SelectSingleNode("/configuration/" + section); if ( sectionNode != null ) { sectionNode.RemoveAll(); } else { sectionNode = document.CreateElement(section); XmlNode parent = document.SelectSingleNode("/configuration"); parent.AppendChild(sectionNode); } // imports the new node to the document XmlNode cloneNode = document.ImportNode(node, true); sectionNode.AppendChild(cloneNode); readFile.Close(); } XmlTextWriter writer = null; if ( !supportsNamespaces ) { writer = new SkipSerializerNamespacesWriter(configFile, null); } else { writer = new XmlTextWriter(configFile, null); } writer.Formatting = Formatting.Indented; writer.Namespaces = supportsNamespaces; document.WriteTo(writer); writer.Flush(); writer.Close(); }