程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> C#裡將模型對象序列化成XML並存入Access庫

C#裡將模型對象序列化成XML並存入Access庫

編輯:.NET實例教程

即可以序列化成文本也可以序列化至文件.

using  System;
using  System.Text;
using  System.XML;
using  System.XML.Serialization;
using  System.IO;
namespace  CS.Common.Utility
...{
        /**////  <summary>
        ///  序列化與反序列化
        ///  FileName                        :        Serializer.cs      
        ///  Verion                          :              0.10
        ///  Author                          :              zhouyu   http://max.cszi.com
        ///  Update                        :              2007-10-22
        ///  Description            :              序列化與反序列化,主要用於將對象裡的數據序列化成XML數據,用於存於文本或是數據庫
        ///  Thanks                        :              小浩http://cs.alIEnwave.cn  )
        ///  </summary>
        public  class  Serializer

  ...{
                /**////  <summary>
                ///  將文件反序列化成對象
                ///  使用:BlogSettingInfo  info  =    (BlogSettingInfo)  Serializer.XmlDeserializerFormFile(typeof(BlogSettingInfo),  @"H:CSBlog.XML");
                ///  </summary>
                ///  <param  name="type">對象類型</param>
                ///  <param  name="path">文件路徑</param>
                ///  <returns></returns>
                public  static  object  XMLDeserializerFormFile(Type  type,  string  path)
                ...{
                        return  new  XmlSerializer(type).Deserialize(new  XMLTextReader(path));
                }


                /**////  <summary>
                ///  將字符串內容反序列化成對象
          &nbsp;     ///  使用:BlogSettingInfo  info  =  (BlogSettingInfo)Serializer.XMLDeserializerFormText(typeof(BlogSettingInfo),config);
                ///  </summary>
                ///  <param  name="type">對象類型</param>
                ///  <param  name="serializeText">被序列化的文本</param>
                ///  <returns></returns>
                public  static  object  XMLDeserializerFormText(Type  type,  string  serializeText)
                ...{
                        using  (StringReader  reader  =  new  StringReader(serializeText))
                        ...{
                                return  new  XMLSerializer(type).Deserialize(reader);
                        }
                }


                /**////  <summary>
                ///  將目標對象序列化成XML到文件中
///  </summary>
                ///  <param  name="target"></param>
                ///  <param  name="path"></param>
                public  static  void  XMLSerializerToFile(object  target,  string  path)
                ...{
                        //XmlTextWriter  writer  =  new  XMLTextWriter(path,  Encoding.UTF8);
                        StreamWriter  writer  =  new  StreamWriter(path);
                        new  XMLSerializer(target.GetType()).Serialize((StreamWriter)writer,  target);
                        writer.Close();
                }


                /**////  <summary>
                ///  將目標對象序列化成完整的XML文檔
                ///  </summary>
                ///  <param  name="target"></param>
                ///  <returns></returns>
                public  static  string  XmlSerializerToXML(object  target)
          ...{
                        return  XMLSerializerToText(target,  false);
                }


                /**////  <summary>
                ///  將目標對象序列化成XML文檔內容(去除聲明屬性)
                ///  </summary>
                ///  <param  name="target"></param>
                ///  <returns></returns>
                public  static  string  XMLSerializerToText(object  target)
                ...{
                        return  XMLSerializerToText(target,  true);
                }


                /**////  <summary>
                ///  將目標對象序列化成XML文檔
                ///  </summary>
                ///  <param  name="target"></param>
                ///  <param  name="isText">是否去除聲明屬性</param>
                ///  <returns></returns>
                private  static  string  XMLSerializerToText(object  target,  bool  isText)
                ...{
                        StringWriter  writer  =  new  StringWriter();
                        new  XMLSerializer(target.GetType()).Serialize((TextWriter)writer,  target);
                        StringBuilder  sb  =  writer.GetStringBuilder();
                        writer.Close();
                        if  (isText)
                        ...{
                                sb.Replace("<?XML  version="1.0"  encoding="utf-16"?> ",  "");
                                sb.Replace("  xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"",  "");
                        }
                        else
                ...{
                                sb.Replace("utf-16",  "utf-8");
                        }
                        return  sb.ToString();
                }

        }

  

}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved