几个用于序列化的代码片段

参考
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));




转载于:https://www.cnblogs.com/wdfrog/archive/2011/11/08/2240657.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/275188.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

桌面图标摆放图案_用图标制作醒目的图案

桌面图标摆放图案Level up your video calls with a custom backdrop created using Noun Project icons.使用使用Noun Project图标创建的自定义背景来升级视频通话。 The only thing more visually pleasing than a well-designed icon is a neat, eye-catching pattern made…

3个多月,近3000人参与的源码共读,诚邀加入~

大家好&#xff0c;我是若川。众所周知&#xff0c;从8月份开始&#xff0c;我组织了源码共读活动&#xff0c;每周学习200行左右的源码&#xff0c;到现在持续了3个多月&#xff0c;坚持答疑解惑。帮助了不少人&#xff0c;还是挺开心的。另外&#xff0c;涌现了很多优秀的读者…

upc 组队赛18 STRENGTH【贪心模拟】

STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challenges or doubts. Feel the fear and do it anyway! If you have been going through a rough time and feel burnt out or stressed, the Strength card encourag…

JSONNull

最近用JSONObject&#xff0c;感觉比xml好用一些&#xff0c;json的打包和解包都比较清晰和容易&#xff0c;最近遇到一个问题&#xff0c;将一个JSON对象解析&#xff0c;存到hashmap中去&#xff0c;然后再从hashmap取出数据&#xff0c;遇到jsonnull的问题&#xff0c;本以为…

“这张图告诉你什么?”

For data to be impactful, it must be understood.为了使数据具有影响力&#xff0c;必须理解它。 I’ve happily spent hundreds and hundreds of hours of my life watching users misunderstand data visualizations. I’m strangely hooked on it.我快乐地度过了数百个小…

我们从 UmiJS 迁移到了 Vite

大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以点此加我微信ruochuan12 进群参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行三个月了&#xff0c;很多小伙伴表示收获颇丰。我们从 UmiJS迁移到 Vite 已经上线半年…

将DataTable的内容以EXCEl的形式导出到本地

1.在搞项目的时候一般会遇到&#xff0c;将GridView或者Repeater的内容以Excel的形式保存到本地&#xff0c;即导出功能。我总结了两个方法。 方法一&#xff1a; 1 DataTable dt query.GetItems().GetDataTable();2 if (dt ! null)3 {4 …

智能家居数据库设计_设计更智能的数据表

智能家居数据库设计重点 (Top highlight)Data tables are hard. There are many different ways to think about them. So, naturally, the first step would be to figure out what your users need.数据表很难。 有许多不同的方式来考虑它们。 因此&#xff0c;自然地&#x…

可能是全网首个前端源码共读活动,诚邀你加入一起学习

大家好&#xff0c;我是若川。众所周知&#xff0c;从8月份开始&#xff0c;我组织了源码共读活动&#xff0c;每周学习200行左右的源码&#xff0c;到现在持续了3个多月&#xff0c;坚持答疑解惑。帮助了不少人&#xff0c;还是挺开心的。另外&#xff0c;涌现了很多优秀的读者…

vsftpd 的配置项目

基本配置说明&#xff1a; 1&#xff09;local_root/ftpfile(当本地用户登入时&#xff0c;将被更换到定义的目录下&#xff0c;默认值为各用户的家目录) 2&#xff09;anon_root/ftpfile(使用匿名登入时&#xff0c;所登入的目录) 3&#xff09;use_localtimeYES(默认是GMT时…

线段树专辑——pku 3667 Hotel

http://poj.org/problem?id3667 哈哈&#xff0c;经典中的经典题啊。利用线段树求最大连续空闲区间&#xff0c;并返回空闲区间的起点坐标。 View Code 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 using namespace std; 5 6 …

houseparty不流畅_重新设计Houseparty –用户体验案例研究

houseparty不流畅Houseparty has become very popular during the COVID-19 period because it helps you connect with others in a fun way. The concept is simple, you open the app and jump on a video call with your friends. You can even play online games with the…

你不知道的 Node.js 工具函数

从类型判断说起在 JavaScript 中&#xff0c;进行变量的类型校验是一个非常令人头疼的事&#xff0c;如果只是简单的使用 typeof 会到各种各样的问题。举几个简单的&#x1f330;&#xff1a;console.log(typeof null) // object console.log(typeof new Array) // object cons…

Java应用集群下的定时任务处理方案(mysql)

今天来说一个Java多机部署下定时任务的处理方案。 需求: 有两台服务器同时部署了同一套代码&#xff0c; 代码中写有spring自带的定时任务&#xff0c;但是每次执行定时任务时只需要一台机器去执行。 当拿到这个需求时我脑子中立马出现了两个简单的解决方案&#xff1a; 利用ip…

概念验证_设置成功的UX概念验证

概念验证用户体验/概念证明/第1部分 (USER EXPERIENCE / PROOF OF CONCEPT / PART 1) This is the first article of a four-part series. Please read Part 2 and Part 3.这是由四个部分组成的系列文章的第一篇。 请阅读 第2 部分 和 第3部分 。 How do today’s top UX desi…

从 vue3 和 vite 源码中,我学到了一行代码统一规范团队包管理器的神器

1. 前言大家好&#xff0c;我是若川。最近组织了源码共读活动&#xff0c;感兴趣的可以加我微信 ruochuan12 参与&#xff0c;每周大家一起学习200行左右的源码&#xff0c;共同进步。已进行四个月了&#xff0c;很多小伙伴表示收获颇丰。想学源码&#xff0c;极力推荐之前我写…

什么事接口

假设你设计一个和人交流的程序。 先建立一个接口 interface 人 //定义接口&#xff0c;它代表一个人&#xff0c; {void Hello(); }//接口虚函数&#xff0c;用来跟这个人说话 但不同的人有不用的交流方式&#xff0c;具体方式用类来实现&#xff0c;比如。 class 美国人&#…

6个高效办公的Excel小技巧,学会让你高效办公

很多人在做Excel表格的时候&#xff0c;会出现下面这种情况&#xff1a;好不容易把内容都输入好了&#xff0c;才发现文字或是数字的排列组合需要重新调整&#xff0c;这个时候头就大了&#xff0c;到底是要一个个复制黏贴&#xff0c;还是要删除后再添加&#xff1f;不管哪种方…

unity 完美像素_像素完美

unity 完美像素从Kidpix到设计系统 (From Kidpix to design systems) Did you ever create stamps in KidPix? Kidpix is bitmap drawing software that’s been around since the nineties, and I remember many happy — more like maddening — hours creating tiny pixela…

整整4个月了,尽全力组织了源码共读活动~

大家好&#xff0c;我是若川。从8月份到现在11月结束了。每周一期&#xff0c;一起读200行左右的源码&#xff0c;撰写辅助文章&#xff0c;截止到现在整整4个月了。由写有《学习源码整体架构系列》20余篇的若川【若川视野公众号号主】倾力组织&#xff0c;召集了各大厂对于源码…