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(1999, 1, 1, 0, 0, 0);
                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;
        }

文章出处:http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2008430/112161_2.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值