.net 是否可以从内存中的XML字符串加载.config文件而不写入该文件?
我知道如何使用ConfigurationFileMap类和ConfigurationManager.OpenMappedMachineConfiguration从文件中加载配置对象,但是有没有办法仅从XML加载配置对象?是的。此帮助器类应允许您在xml中读取或写入任何对象,包括ConfigurationManager对象.net 是否可以从内存中的XML字符串加载.config文件而不写入该文件?,.net,configuration,.net,Configuration,我知道如何使用ConfigurationFileMap类和ConfigurationManager.OpenMappedMachineConfiguration从文件中加载配置对象,但是有没有办法仅从XML加载配置对象?是的。此帮助器类应允许您在xml中读取或写入任何对象,包括ConfigurationManager对象 public static class XmlUtility { /// <summary> /// Serializes an object to
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);
}
}
}
问题是-您无法反序列化任何与配置相关的对象-尝试过,但失败了。它们包含成员变量和反序列化失败的其他内容。。。。。这在一般情况下有效,但在这种特殊情况下,它将不起作用:-