zoukankan      html  css  js  c++  java
  • 几个用于序列化的代码片段

    参考
    JavaScriptSerializer,一般用来做JSON格式化
    http://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptserializer.aspx

    http://msdn.microsoft.com/zh-cn/library/system.web.script.serialization.javascriptconverter.aspx

    DataContractSerializer,可以用来做XML,JSON格式化,可以将格式化内容直接写入流
    http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.datacontractserializer.aspx

    代码片段:

    View Code
    //**************序列化实体**********************
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml.Serialization;

    namespace PM.WebForm.common
    {
    ///<summary>
    /// 服务器列表
    ///</summary>
    [XmlRoot("Root")]
    public class serverlist
    {
    ///<summary>
    /// 服务器IP
    ///</summary>
    [XmlAttribute("serverip")]
    public string serverip { set; get; }
    ///<summary>
    /// 服务器域名,如http://www.163.com/
    ///</summary>
    [XmlAttribute("server_url")]
    public string server_url { set; get; }
    ///<summary>
    /// s_key,服务器间加密s_key,很重要
    ///</summary>
    [XmlAttribute("s_key")]
    public string s_key { set; get; }
    ///<summary>
    /// 合作编号
    ///</summary>
    [XmlAttribute("pid")]
    public string pid { set; get; }
    ///<summary>
    /// 注册渠道ID
    ///</summary>
    [XmlAttribute("sid")]
    public string sid { set; get; }
    ///<summary>
    /// 服务器充值接口url
    ///</summary>
    [XmlAttribute("server_pay_url")]
    public string server_pay_url { set; get; }
    ///<summary>
    /// 服务器登陆接口
    ///</summary>
    [XmlAttribute("server_login_url")]
    public string server_login_url { set; get; }
    ///<summary>
    /// 服务器ID号,用来唯一标识
    ///</summary>
    [XmlAttribute("serverid")]
    public string serverid { set; get; }
    ///<summary>
    /// 第几区
    ///</summary>
    [XmlAttribute("area")]
    public string area { set; get; }
    ///<summary>
    /// 第几服
    ///</summary>
    [XmlAttribute("fu")]
    public string fu { set; get; }
    ///<summary>
    /// 显示名称
    ///</summary>
    [XmlAttribute("showname")]
    public string showname { set; get; }
    ///<summary>
    /// 详细介绍
    ///</summary>
    [XmlAttribute("detail")]
    public string detail { set; get; }

    ///<summary>
    /// 君主排行
    ///</summary>
    [XmlAttribute("xml1")]
    public string xml1 { set; get; }

    ///<summary>
    /// 联盟排行
    ///</summary>
    [XmlAttribute("xml2")]
    public string xml2 { set; get; }


    }
    }
    //*************序列化帮助类*************
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml.Serialization;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Xml;



    ///<summary>
    ///序列化与反序列化对象
    ///</summary>
    public class Serializator
    {

    public static string SerializeLinqList<T>(List<T> list)
    {

    DataContractSerializer dcs = new DataContractSerializer(typeof(List<T>));

    StringBuilder sb = new StringBuilder();

    using (XmlWriter writer = XmlWriter.Create(sb))
    {

    dcs.WriteObject(writer, list);

    }

    return sb.ToString();

    }



    public static List<T> DeserializeLinqList<T>(string xml)
    {

    List<T> list;
    DataContractSerializer dcs = new DataContractSerializer(typeof(List<T>));
    using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
    {

    list = dcs.ReadObject(reader) as List<T>;

    }

    if (list == null) list = new List<T>();

    return list;

    }

    }

    DataContractSerializer进行JSON格式化

    View Code
        [DataContract]
    public class DataTreeNode<T> where T: new()
    {
    [DataMember]
    public T Data { get; set; }
    [DataMember]
    public List<DataTreeNode<T>> Children { get; set; }
    public DataTreeNode()
    {
    Data = new T();
    Children = new List<DataTreeNode<T>>();
    }
    public DataTreeNode(T data)
    {
    Data = data;
    Children = new List<DataTreeNode<T>>();
    }

    public DataTreeNode<T> Parent { get; set; }
    ///<summary>
    /// 子类中标记为Enabled的节点数
    ///</summary>
    public int EnabledChildCount { get; set; }

    }
    public static class Extensions
    {
    ///<summary>
    /// 序列化UTF-8
    ///</summary>
    ///<param name="item"></param>
    ///<returns></returns>
    public static string ToJson(this object item)
    {
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());
    using (MemoryStream ms = new MemoryStream())
    {
    serializer.WriteObject(ms, item);
    StringBuilder sb = new StringBuilder();
    sb.Append(Encoding.UTF8.GetString(ms.ToArray()));
    return sb.ToString();
    }
    }


    }

    javaScriptSerializer进行Json格式化(导入命名空间:System.Web.Script.Serialization;)

    View Code
                JavaScriptSerializer serializer = new JavaScriptSerializer();
    AreaClassManager manager = OB.R<AreaClassManager>();
    var area = manager.Query("ClassId,ClassName,Code");
    List<AreaClass> model = new List<AreaClass>();
    area.ForEach(ent =>
    {
    string code = ent.Code.Length != 4 ? (ent.Code.Substring(0, ent.Code.Length - 4)) : "0001";
    int level = ent.Code.Length / 4;
    model.Add(new AreaClass
    {
    I = ent.ClassID,
    C = ent.ClassName,
    P = manager.GetModelList("Code='" + code + "'")[0].ClassID,
    L=level
    });

    });

    return Content("var G_AreaList=" + serializer.Serialize(model));




  • 相关阅读:
    zabbix 对/etc/ssh/sshd_config文件的监控 但status为unknowen
    Kotlin从零到精通Android开发
    谷歌官方 构建您的第一个应用 Kotlin版
    android studio 运行按钮为灰色的解决办法之一
    webapi发布到windows 2012的iis8里 出错
    Asp.net MVC WebApi项目的自动接口文档及测试功能打开方法
    Asp.net Web Api开发(第四篇)Help Page配置和扩展
    关于SNMP的MIB文件的语法简述
    Visual Stdio 2017增加SVN支持
    ffmpeg 多个音频合并 截取 拆分
  • 原文地址:https://www.cnblogs.com/wdfrog/p/2240657.html
Copyright © 2011-2022 走看看