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;
            }
        }
    }
    

      

  • 相关阅读:
    c#之字段,属性,索引器,常量
    c#类,对象,类成员简介
    c#之接口,依赖反转,单元测试
    c#之 抽象类与开闭原则
    c#重写与多态
    c#之类
    c#之类的继承,类成员的访问控制
    c#之委托
    c# try catch用法思路
    js的全局变量
  • 原文地址:https://www.cnblogs.com/sportdog/p/10750513.html
Copyright © 2011-2022 走看看