public class XmlUtility { /// <summary> /// 反序列化.接收2個參數:xmlFilePath(需要反序列化的XML文件的絕對路徑),type(反序列化XML為哪種對象類型) /// </summary> /// <param name="xmlFilePat ...
public class XmlUtility
{
/// <summary>
/// 反序列化.接收2個參數:xmlFilePath(需要反序列化的XML文件的絕對路徑),type(反序列化XML為哪種對象類型)
/// </summary>
/// <param name="xmlFilePath"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object DeserializeFromXml(string xmlFilePath, Type type)
{
object result = null;
if (File.Exists(xmlFilePath))
{
using (StreamReader reader = new StreamReader(xmlFilePath))
{
XmlSerializer xs = new XmlSerializer(type);
result = xs.Deserialize(reader);
}
}
return result;
}
/// <summary>
/// 序列化.
///接收4個參數:srcObject(對象的實例),type(對象類型),xmlFilePath(序列化之後的xml文件的絕對路徑),xmlRootName(xml文件中根節點名稱)
///當需要將多個對象實例序列化到同一個XML文件中的時候,xmlRootName就是所有對象共同的根節點名稱,如果不指定,.net會預設給一個名稱(ArrayOf+實體類名稱)
/// </summary>
/// <param name="srcObject"></param>
/// <param name="type"></param>
/// <param name="xmlFilePath"></param>
/// <param name="xmlRootName"></param>
public static void SerializeToXml(object srcObject, Type type, string xmlFilePath, string xmlRootName)
{
if (srcObject != null && !string.IsNullOrEmpty(xmlFilePath))
{
type = type != null ? type : srcObject.GetType();
using (StreamWriter sw = new StreamWriter(xmlFilePath))
{
XmlSerializer xs = string.IsNullOrEmpty(xmlRootName) ?
new XmlSerializer(type) :
new XmlSerializer(type, new XmlRootAttribute(xmlRootName));
xs.Serialize(sw, srcObject);
}
}
}
}
以上為序列化與反序列化方法寫的類
調用以上方法:
//反序列化方法調用
private SMSConfig GetSmsConfig()
{
//獲取物理路徑,下麵方法是項目中單獨寫的東西,這個可能需要讀者自己寫一下
var xmlFilePath = WebUtility.GetPhysicalFilePath("~/Config/SystemConfig/SMS.config");
//調用反序列化方法(SMSConfig是根據序列化節點創建的model)
return XmlUtility.DeserializeFromXml(xmlFilePath, typeof(SMSConfig)) as SMSConfig;
}
//序列化方法調用
public JsonResult SaveSMSConfig(SystemConfigModel model)
{
var xmlFilePath = WebUtility.GetPhysicalFilePath("~/Config/SystemConfig/SMS.config");
//調用序列化方法(SMSConfig是根據序列化節點創建的model.cs)("SMS"是根節點)
XmlUtility.SerializeToXml(model.SMSConfig, typeof(SMSConfig), xmlFilePath, "SMS");
}
重點看兩個方法調用時,參數的不同。
xml文件是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<SMS xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RestIp>https://www.baidu.com/</RestIp>
<RestPort>83</RestPort>
<MainAccount>8a48b5514b0b42909cf120d</MainAccount>
<MainToken>30bbe361564b98ac4e4cb04f795</MainToken>
<AppId>8a48b5514b04b242ea5201228</AppId>
<RegistTemplateId>244</RegistTemplateId>
<RegistSuccessTemplateId>249</RegistSuccessTemplateId>
<ResetPasswordTemplateId>250</ResetPasswordTemplateId>
<NotifyTemplateId>249</NotifyTemplateId>
</SMS>
根據xml文件創建model.cs:
[XmlRoot(ElementName = "SMS")]
public class SMSConfig
{
[Required(ErrorMessage = "請輸入RestIp")]
[XmlElement(ElementName = "RestIp")]
public string RestIp { get; set; }
[Required(ErrorMessage = "請輸入RestPort")]
[XmlElement(ElementName = "RestPort")]
public string RestPort { get; set; }
[Required(ErrorMessage = "請輸入MainAccount")]
[XmlElement(ElementName = "MainAccount")]
public string MainAccount { get; set; }
[Required(ErrorMessage = "請輸入MainToken")]
[XmlElement(ElementName = "MainToken")]
public string MainToken { get; set; }
[Required(ErrorMessage = "請輸入AppId")]
[XmlElement(ElementName = "AppId")]
public string AppId { get; set; }
[Required(ErrorMessage = "請輸入RegistTemplateId")]
[XmlElement(ElementName = "RegistTemplateId")]
public string RegistTemplateId { get; set; }
[Required(ErrorMessage = "請輸入RegistSuccessTemplateId")]
[XmlElement(ElementName = "RegistSuccessTemplateId")]
public string RegistSuccessTemplateId { get; set; }
[Required(ErrorMessage = "請輸入ResetPasswordTemplateId")]
[XmlElement(ElementName = "ResetPasswordTemplateId")]
public string ResetPasswordTemplateId { get; set; }
[Required(ErrorMessage = "請輸入NotifyTemplateId")]
[XmlElement(ElementName = "NotifyTemplateId")]
public string NotifyTemplateId { get; set; }
}