I know how to use the ConfigurationFileMap class and ConfigurationManager.OpenMappedMachineConfiguration to load a Configuration object from a file, but is there a way to just load a Configuration object from purely the XML?
views:
116answers:
1
A:
Yes. This helper class should allow you to read or write any object to/from xml, including your ConfigurationManager object.
public static class XmlUtility
{
/// <summary>
/// Serializes an object to an XML string.
/// </summary>
public static string ToXML(object Obj)
{
Type T = Obj.GetType();
XmlSerializer xs = new XmlSerializer(T);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
xs.Serialize(ms, Obj);
UTF8Encoding ue = new UTF8Encoding();
return ue.GetString(ms.ToArray());
}
}
/// <summary>
/// Deserializes an object from an XML string.
/// </summary>
public static T FromXML<T>(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(xml))
{
return (T)xs.Deserialize(sr);
}
}
}
Robert Harvey
2009-06-04 23:11:47
Trouble is - you can't deserialize any of the configuration related objects - tried that, failed at it. They contain stuff (member variables and other things) that fail on deserialization..... this works in general, but in this particular case, it won't work :-(
marc_s
2009-06-05 05:21:38