zoukankan      html  css  js  c++  java
  • 将对象序列化成XML字符串

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace TestWeb
    {
        public partial class TestSerializeXml : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnSerialize_Click(object sender, EventArgs e)
            {
                Person p = new Person();
                p.Name = "Snow";
                p.Age = 41;
                string str = this.Serialize(p);
                this.TextBox1.Text = str;
            }
    
            protected void btnDeSerialize_Click(object sender, EventArgs e)
            {
                Person p = this.Deserialize(this.TextBox1.Text);
                Response.Write(p.Name + "*" + p.Age);
            }
    
            public Person Deserialize(String xml)
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(new Person().GetType());
                System.IO.StringReader sr = new System.IO.StringReader(xml);
    
                Person person = xs.Deserialize(sr) as Person;
                sr.Close();
                return person;
            }
    
            private string Serialize(Person person)
            {
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(person.GetType());
                
                System.IO.StringWriter sw = new System.IO.StringWriter();
                xs.Serialize(sw, person);
                String s = sw.ToString();
                sw.Close();
                return s;
            }
        }
    }
    

      

  • 相关阅读:
    Linux启动mysql命令
    Linux启动Apache服务器命令
    使用SSH命令从一台Linux远程登陆到另一台Linux
    Linux关机命令
    从Windows复制文件到Linux
    无法访问SVN历史记录的问题
    linux静态IP最简配置
    学习之Redis(二)
    学习之Redis(一)
    MySQL数据库笔记总结
  • 原文地址:https://www.cnblogs.com/sportdog/p/10750513.html
Copyright © 2011-2022 走看看