c#序列化與反序列化

<?xml version="1.0"?>
<Gather_Article xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  
<Title>xxxxx </Title>
  
<UrlSource>http://www.shenhua.com/html/yaowenkuaixun/yaowenkuaixun/20080423/5378.html</UrlSource>
  
<Resource>國華投資公司 </Resource>
  
<Content>
    
     
</Content>
  
<IsSave>0</IsSave>
  
<Picture>
    
<string>http://www.shenhua.com/upimg/userup/0804/231020432o6.jpg</string>
  
</Picture>
  
<Author>李少寶 </Author>
  
<Date>2008-04-23</Date>
</Gather_Article>
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Web;
using amao.Config;
using System.Web.Caching;

namespace Entity
{
    
/// <summary>
    
/// 實體類Gather_Article 。(屬性說明自動提取數據庫字段的描述信息)
    
/// </summary>
    
/// 
    [Serializable()]
    
public class Gather_Article
    {
        
public Gather_Article()
        {
        }

        
#region Model
        
/// <summary>
        
/// 文章標題
        
/// </summary>
        public string Title
        {
            
set;
            
get;
        }
        
public string UrlSource
        {
            
get;
            
set;
        }
        
/// <summary>
        
/// 文章說明、來源
        
/// </summary>
        public string Resource
        {
            
set;
            
get;
        }
        
/// <summary>
        
/// 文章正文
        
/// </summary>
        public string Content
        {
            
set;
            
get;
        }
        
/// <summary>
        
/// 是否保存
        
/// </summary>
        public int IsSave
        {
            
set;
            
get;
        }
        
/// <summary>
        
/// 圖片鏈接地址
        
/// </summary>
        public List<string> Picture
        {
            
set;
            
get;
        }

        
public DateTime IssueDate
        {
            
get
            {
                DateTime dt;
                
if (!DateTime.TryParse(Date, out dt))
                    dt
=new DateTime(199911000);
                
return dt;
            }
        }
        
public string Author
        {
            
get;
            
set;
        }
        
/// <summary>
        
/// 發佈時間
        
/// </summary>
        public string Date
        {
            
get;
            
set;
        }
        
public bool Save()
        {
            
try
            {
                
string filepath=Utils.FileBasePath+"/"+this.Title.Trim().Replace("/""").Replace("/""")+DateTime.Now.ToString("_MMddHHmmss")+".XML";
                XmlSerializer xs
=new XmlSerializer(typeof(Gather_Article));
                FileInfo f
=new FileInfo(filepath);
                
if (!f.Directory.Exists)
                    f.Directory.Create();
                Stream stream
=new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.Read);
                xs.Serialize(stream, 
this);
                stream.Close();
                
return true;
            }
            
catch (System.Exception ex)
            {
                amao.Chart.Log.WriteLog(ex.ToString());
                
return false;
            }
        }
        
public static Gather_Article Read(string filepath)
        {
            
if (HttpRuntime.Cache[filepath]==null)
            {
                Gather_Article gobj
=new Gather_Article();
                
if (File.Exists(filepath))
                {
                    XmlSerializer xs
=new XmlSerializer(typeof(Gather_Article));
                    Stream stream
=new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    gobj
=xs.Deserialize(stream) as Gather_Article;
                    stream.Close();
                }
                
else
                {
                    
return null;
                }
                HttpRuntime.Cache.Insert(
                       filepath,
                       gobj,
                       
new CacheDependency(filepath),
                       System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromDays(
1)
                       );
                
return gobj;
            }
            
else
            {
                
return HttpRuntime.Cache[filepath] as Gather_Article;
            }
        }
/*      */
        
#endregion Model

    }
}

     也可以在類中添加帶這樣標記的屬性,而後在xml中會以 Gather_Article的屬性出現 <Gather_Article Url="" Coding=""
  [XmlAttribute("Url")]
        
public string Url
        {
            
get;
            
set;
        }
        [XmlAttribute(
"Coding")]
        
public string Coding
        {
            
get;
            
set;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章