• 序列化和发序列化


    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;

  • 相关阅读:
    iOS面试题03-UI控件
    iOS面试题02-数据存储
    iOS面试题01-多线程网络
    ios开发学习笔记(1)
    搭建Myeclipse下Java Web开发环境
    iOS开发-UI (十二)StoryBoard
    iOS开发-UI (十一) UITabBarController
    iOS开发-UI (十)UIScrollView 和 UIPageControl使用
    iOS开发-UI (九)UITableView搜索功能
    iOS开发-UI (八)TableView
  • 原文地址:https://www.cnblogs.com/weibozeng/p/3517538.html
Copyright © 2020-2023  润新知