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

 

你可能感兴趣的:(C++,c,C#,asp.net,asp)