/// <summary>
    /// 序列化对象为xml字符串
    /// </summary>
    /// <param name="obj">要序列化的对象</param>
    /// <returns>xml格式字符串</returns>
    public static string Serialize(this object obj)
    {
        if (obj == null) { return ""; }
        Type type = obj.GetType();
        if (type.IsSerializable)
        {
            try
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
                XmlWriterSettings xset = new XmlWriterSettings();
                xset.CloseOutput = true;
                xset.Encoding = Encoding.UTF8;
                xset.Indent = true;
                xset.CheckCharacters = false;
                System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb, xset);
                xs.Serialize(xw, obj);
                xw.Flush();
                xw.Close();
                return sb.ToString();
            }
            catch { return ""; }
        }
        else
        {
            return "";
        }
    }

相关文章:

  • 2021-05-22
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-04
  • 2021-11-27
  • 2021-07-14
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2022-01-26
相关资源
相似解决方案