ExtJs XMLHelper XML序列化與反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace CommonHelper
{
    
public class Serialize
    
{
        
/// <summary>
        
/// 序列化對象
        
/// </summary>
        
/// <typeparam name="T">對象類型</typeparam>
        
/// <param name="t">對象</param>
        
/// <returns></returns>

        public static string XmlSerialize<T>(T t)
        
{
            
using (StringWriter sw = new StringWriter())
            
{
                XmlSerializer xz 
= new XmlSerializer(t.GetType());
                xz.Serialize(sw, t);
                
return sw.ToString();
            }

        }


        
/// <summary>
        
/// 序列化對象
        
/// </summary>
        
/// <typeparam name="T">對象類型</typeparam>
        
/// <param name="t">對象</param>
        
/// <returns></returns>

        public static string XmlSerialize(object t)
        
{
            
using (StringWriter sw = new StringWriter())
            
{
                XmlSerializer xz 
= new XmlSerializer(t.GetType());
                xz.Serialize(sw, t);
                
return sw.ToString();
            }

        }


        
/// <summary>
        
/// 反序列化爲對象
        
/// </summary>
        
/// <param name="type">對象類型</param>
        
/// <param name="s">對象序列化後的Xml字符串</param>
        
/// <returns></returns>

        public static T XmlDeserialize<T>(Type type, string s)
        
{
            
using (StringReader sr = new StringReader(s))
            
{
                XmlSerializer xz 
= new XmlSerializer(type);
                
return (T)xz.Deserialize(sr);
            }

        }


    }

}

 
發佈了67 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章