XML序列化与反序列化

2018-06-22 06:09:37来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:自动化CodeReview - ASP.NET Core请求参数验证

下一篇:Swagger 生成API文档