序列化 与 反序列化 字符串 实例

转自: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" />&nbsp;<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();
        }

    }

}

原文地址:https://www.cnblogs.com/wangpei/p/1522052.html