zoukankan
html css js c++ java
序列化 与 反序列化 字符串 实例
转自:
http://www.cnblogs.com/sky1024/articles/917108.html
一个实体类
[Serializable]
public
class
MyObject
{
public
MyObject()
{
x
=
y
=
height
=
width
=
0
;
}
private
int
x;
private
int
y;
private
int
height;
private
int
width;
private
string
name;
public
string
Name
{
get
{
return
name;
}
set
{
name
=
value;
}
}
public
int
Height
{
get
{
return
height;
}
set
{
height
=
value;
}
}
public
int
Width
{
get
{
return
width;
}
set
{
width
=
value;
}
}
public
int
X
{
get
{
return
x;
}
set
{
x
=
value;
}
}
public
int
Y
{
get
{
return
y;
}
set
{
y
=
value;
}
}
}
Aspx 页面
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
asp:TextBox
ID
="TextBox1"
runat
="server"
></
asp:TextBox
><
br
/>
<
asp:TextBox
ID
="TextBox2"
runat
="server"
></
asp:TextBox
><
br
/>
<
asp:TextBox
ID
="TextBox3"
runat
="server"
></
asp:TextBox
><
br
/>
<
asp:TextBox
ID
="TextBox4"
runat
="server"
></
asp:TextBox
><
br
/>
<
asp:TextBox
ID
="TextBox5"
runat
="server"
></
asp:TextBox
><
br
/>
<
br
/>
<
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="序列化"
Width
="75px"
/>
<
br
/>
<
br
/>
<
asp:Label
ID
="Label1"
runat
="server"
></
asp:Label
><
br
/>
<
br
/>
<
asp:Button
ID
="Button2"
runat
="server"
OnClick
="Button2_Click"
Text
="反序列化"
/><
br
/>
<
br
/>
<
asp:TextBox
ID
="TextBox6"
runat
="server"
></
asp:TextBox
>
<
br
/>
<
asp:TextBox
ID
="TextBox7"
runat
="server"
></
asp:TextBox
><
br
/>
<
asp:TextBox
ID
="TextBox8"
runat
="server"
></
asp:TextBox
><
br
/>
<
asp:TextBox
ID
="TextBox9"
runat
="server"
></
asp:TextBox
><
br
/>
<
asp:TextBox
ID
="TextBox10"
runat
="server"
></
asp:TextBox
></
div
>
</
form
>
</
body
>
</
html
>
cs页面代码:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Xml;
using
System.Xml.Serialization;
using
System.IO;
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
MyObject myobj
=
new
MyObject();
myobj.Name
=
TextBox1.Text;
myobj.Height
=
Convert.ToInt32(TextBox2.Text);
myobj.Width
=
Convert.ToInt32(TextBox3.Text);
myobj.X
=
Convert.ToInt32(TextBox4.Text);
myobj.Y
=
Convert.ToInt32(TextBox5.Text);
Label1.Text
=
Serialiaze(myobj);
}
/**/
///
<summary>
///
序列化成字符串
///
</summary>
///
<param name="obj"></param>
///
<returns>
序列化后的字符串
</returns>
public
static
string
Serialiaze(
object
obj)
{
XmlSerializer xs
=
new
XmlSerializer(obj.GetType());
MemoryStream ms
=
new
MemoryStream();
XmlTextWriter xtw
=
new
XmlTextWriter(ms, System.Text.Encoding.UTF8);
xtw.Formatting
=
Formatting.Indented;
xs.Serialize(xtw, obj);
ms.Seek(
0
, SeekOrigin.Begin);
StreamReader sr
=
new
StreamReader(ms);
string
str
=
sr.ReadToEnd();
xtw.Close();
ms.Close();
return
str;
}
/**/
///
<summary>
///
反序列化 从字符串
///
</summary>
///
<param name="xml">
xml字符串
</param>
///
<param name="type">
要生成的对象类型
</param>
///
<returns>
反序列化后的对象
</returns>
public
static
object
Deserialize(
string
xml, Type type)
{
XmlSerializer xs
=
new
XmlSerializer(type);
StringReader sr
=
new
StringReader(xml);
object
obj
=
xs.Deserialize(sr);
return
obj;
}
protected
void
Button2_Click(
object
sender, EventArgs e)
{
string
str
=
Label1.Text;
MyObject myobj
=
(MyObject)Deserialize(str,
typeof
(MyObject));
if
(myobj
!=
null
)
{
TextBox6.Text
=
myobj.Name;
TextBox7.Text
=
myobj.Height.ToString();
TextBox8.Text
=
myobj.Width.ToString();
TextBox9.Text
=
myobj.X.ToString();
TextBox10.Text
=
myobj.Y.ToString();
}
}
}
查看全文
相关阅读:
HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
HDU 5455 Fang Fang
hihoCoder 1234 fractal
二叉排序树
最大连续子序列和,最大上升子序列和,最长上升子序列,最长公共子串,最长公共子序列,最长上升公共子序列
C++ string string string string string string string string string string
pip安装第三方库
pip安装时使用国内源,加快下载速度
pip升级命令
instanceof -- JS
原文地址:https://www.cnblogs.com/wangpei/p/1522052.html
最新文章
使用qemu-img创建虚拟磁盘文件
使用命令virsh管理网络设备,创建桥设备 和 使用virt-manager创建虚拟机
安装libvirt管理套件(C/S架构模式,用户管理kvm虚拟机)
在Wmware虚拟机上如何检查是否CPU支持虚拟化 和 加载kvm模块
虚拟化介绍
ansible介绍和安装
jquery中取消和绑定hover事件的正确方式
浅谈Web网站架构演变过程
5.Transact-SQL编程
触发器
热门文章
十步完全理解SQL
mvc5权限管理(简单登录):ActionFilterAttribute
MVC PageList使用(异步 与 正常)
.Net程序员学习Linux最简单的方法
SQL Server系统表sysobjects介绍与使用(转))
MVC5 条件查询异步刷新
电脑使用小技巧
百度经验 怎么在右键上加上用某软件打开
不能选择sublime作为默认打开方式的解决办法
HDU 5478 Can you find it
Copyright © 2011-2022 走看看