zoukankan      html  css  js  c++  java
  • 序列化和发序列化

    1.xml 文档序列化成对象

    public static ContactDetails Deserialize(string proposalsXml)
    {
      try
      {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(ContactDetails));
        TextReader reader = new StringReader(proposalsXml);
        return xmlSerializer.Deserialize(reader) as ContactDetails;
      }
      catch
      {
        throw;
      }
    }

    /// <summary>
    /// Serialize the current object to a string.
    /// </summary>
    /// <returns></returns>
    public string Serialize()
    {
      XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(this.GetType());
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
      //Add an empty namespace and empty value
      ns.Add("", "");
      StringBuilder stringBuilder = new StringBuilder();
      StringWriterWithEncoding textWriter = new StringWriterWithEncoding(stringBuilder, Encoding.UTF8);
      xmlSerializer.Serialize(textWriter, this,ns);
      return stringBuilder.ToString();
    }

    ContactDetails result;
    XmlSerializer ser = new XmlSerializer(typeof(ContactDetails));
    using (TextReader tr = new StringReader(xmlResult.OuterXml.Replace("T00:00:00", string.Empty)))
    {
    result = (ContactDetails)ser.Deserialize(tr);
    }
    return result;

  • 相关阅读:
    bzoj 3339 莫队
    E. XOR and Favorite Number
    HDU 2222 AC自动机
    SPOJ 694 不同子串个数
    Codeforces Round #428 (Div. 2)
    HDU 6103
    《贪婪的动态规划》
    《浅谈图论模型的建立与应用》
    bzoj 2194 快速傅里叶之二
    java中高级面试题整理及参考答案
  • 原文地址:https://www.cnblogs.com/weibozeng/p/3517538.html
Copyright © 2011-2022 走看看