• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            object 序列化

            using ?System;
            using ?System.Xml;
            using ?System.Xml.Serialization;
            using ?System.IO;


            [XmlRoot(
            " productCatalog " )]
            public ? class ?ProductCatalog?
            {

            ????[XmlElement(
            " catalogName " )]
            ????
            public ? string ?CatalogName;
            ????
            ????
            // ?Use?the?date?data?type?(and?ignore?the?time?portion?in?the?
            ????
            // ?serialized?XML).
            ????[XmlElement(ElementName = " expiryDate " ,?DataType = " date " )]
            ????
            public ?DateTime?ExpiryDate;
            ????
            ????
            // ?Configure?the?name?of?the?tag?that?holds?all?products,
            ????
            // ?and?the?name?of?the?product?tag?itself.
            ????[XmlArray( " products " )]
            ????[XmlArrayItem(
            " product " )]
            ????
            public ?Product[]?Products;

            ????
            public ?ProductCatalog()?
            ????
            {
            ????????
            // ?Default?constructor?for?deserialization.
            ????}


            ????
            public ?ProductCatalog( string ?catalogName,?DateTime?expiryDate)?
            ????
            {
            ????????
            this .CatalogName? = ?catalogName;
            ????????
            this .ExpiryDate? = ?expiryDate;
            ????}

            }


            public ? class ?Product?
            {

            ????[XmlElement(
            " productName " )]
            ????
            public ? string ?ProductName;
            ????
            ????[XmlElement(
            " productPrice " )]
            ????
            public ? decimal ?ProductPrice;
            ????
            ????[XmlElement(
            " inStock " )]
            ????
            public ? bool ?InStock;
            ????
            ????[XmlAttributeAttribute(AttributeName
            = " id " ,?DataType = " integer " )]
            ????
            public ? string ?Id;

            ????
            public ?Product()?
            ????
            {
            ????????
            // ?Default?constructor?for?serialization.
            ????}


            ????
            public ?Product( string ?productName,? decimal ?productPrice)?
            ????
            {
            ????????
            this .ProductName? = ?productName;
            ????????
            this .ProductPrice? = ?productPrice;
            ????}

            }

            public ? class ?SerializeXml?
            {

            ????
            private ? static ? void ?Main()?
            ????
            {

            ????????
            // ?Create?the?product?catalog.
            ????????ProductCatalog?catalog? = ? new ?ProductCatalog( " New?Catalog " ,
            ????????????DateTime.Now.AddYears(
            1 ));
            ????????Product[]?products?
            = ? new ?Product[ 2 ];
            ????????products[
            0 ]? = ? new ?Product( " Product?1 " ,? 42.99m );
            ????????products[
            1 ]? = ? new ?Product( " Product?2 " ,? 202.99m );
            ????????catalog.Products?
            = ?products;

            ????????
            // ?Serialize?the?order?to?a?file.
            ????????XmlSerializer?serializer? = ? new ?XmlSerializer( typeof (ProductCatalog));
            ????????FileStream?fs?
            = ? new ?FileStream( " ProductCatalog.xml " ,?FileMode.Create);
            ????????serializer.Serialize(fs,?catalog);
            ????????fs.Close();

            ????????catalog?
            = ? null ;

            ????????
            // ?Deserialize?the?order?from?the?file.
            ????????fs? = ? new ?FileStream( " ProductCatalog.xml " ,?FileMode.Open);
            ????????catalog?
            = ?(ProductCatalog)serializer.Deserialize(fs);

            ????????
            // ?Serialize?the?order?to?the?Console?window.
            ????????serializer.Serialize(Console.Out,?catalog);
            ????????Console.ReadLine();
            ????}

            }



            // store?the?object?in?a?binary?format?

            [Serializable]?

            public ? class ?Item?

            {?

            ?????
            private ? string ?itemName?;?

            ?????
            private ? string ?itemId;?

            ?????
            private ? double ?itemPrice?;?

            ?????
            private ? long ?itemQuantity;?

            ?????
            private ? double ?netAmount;?

            ?????
            public ?Item()?

            ?????
            {?

            ????????????ItemName?
            = ? & quot; & quot;;?

            ????????????ItemPrice?
            = ? 0.00 ;?

            ????????????ItemQuantity?
            = ? 0 ;?

            ????????????netAmount?
            = ? 0.00 ;?

            ?????}
            ?

            ?????
            public ? string ?ItemName?

            ?????
            {?

            ????????????
            get ?

            ????????????
            {?

            ?????????????????
            return ?itemName;?

            ????????????}
            ?

            ????????????
            set ?

            ????????????
            {?

            ?????????????????itemName?
            = ?value;?

            ????????????}
            ?

            ?????}
            ?

            ?????
            public ? string ?ItemId?

            ?????
            {?

            ????????????
            get ?

            ????????????
            {?

            ?????????????????
            return ?itemId;?

            ????????????}
            ?

            ????????????
            set ?

            ????????????
            {?

            ?????????????????itemId?
            = ?value;?

            ????????????}
            ?

            ?????}
            ?

            ?????
            public ? double ?ItemPrice?

            ?????
            {?

            ????????????
            get ?

            ????????????
            {?

            ?????????????????
            return ?itemPrice;?

            ????????????}
            ?

            ????????????
            set ?

            ????????????
            {?

            ?????????????????itemPrice?
            = ?value;?

            ????????????}
            ?

            ?????}
            ?

            ?????
            public ? long ?ItemQuantity?

            ?????
            {?

            ????????????
            get ?

            ????????????
            {?

            ?????????????????
            return ?itemQuantity;?

            ????????????}
            ?

            ????????????
            set ?

            ????????????
            {?

            ?????????????????itemQuantity?
            = ?value;?

            ????????????}
            ?

            ?????}
            ?

            ?????
            public ? double ?NetAmount?

            ?????
            {?

            ????????????
            get ?

            ????????????
            {?

            ?????????????????
            return ?netAmount;?

            ????????????}
            ?

            ????????????
            set ?

            ????????????
            {?

            ?????????????????netAmount?
            = ?value;?

            ????????????}
            ?

            ?????}
            ?

            }
            ?


            // Create?a?file?stream?in?which?teh?object?will?store?

            FileStream?fstream?
            = ?File.Create(SERIALIZED_FILE_PATH? + ?@ & quot;\ & quot;? + ?objItem.ItemId);?

            // Object?will?store?in?binary?format?

            BinaryFormatter?bf?
            = ? new ?BinaryFormatter();?

            // Bind?the?filestream?with?that?of?binaryformatter.?Also?mentioned?the?object?to?save?

            bf.Serialize(fstream,?objItem);?

            fstream.Close();?

            fstream?
            = ? null ;?

            bf?
            = ? null ;?

            // ----------------------------?
            FileStream?fstream? = ?File.OpenRead?(serializedFiles.GetValue(fileIndex).ToString()?);?

            BinaryFormatter?bf?
            = ? new ?BinaryFormatter();?

            Item?ser?
            = ?(Item)?bf.Deserialize(fstream);?

            posted on 2006-03-23 17:21 夢在天涯 閱讀(989) 評論(3)  編輯 收藏 引用 所屬分類: C#/.NET

            評論

            # re: object 序列化 2006-04-14 12:09 夢在天涯

            class must have

            public members or getandset of properities,

            then it can be serialized.  回復  更多評論   

            # re: object 序列化 2006-04-14 16:17 夢在天涯

            NET的對象序列化真是好東西,以后要多多利用。

              在使用.NET的序列化時,碰到過一些問題,還好,有豐富的MSDN可查,沒有什么過不去的檻。在這里,把使用.NET序列化的經驗小結一下。
              1. 基本確認XmlSerializer使用UTF8對序列化的XML文檔編碼。
              2. XmlSerializer只序列化聲明為public的字段,屬性,或帶返回值的方法。
              3. 如果要序列化屬性,那么該屬性必須是可讀寫的,即必須包含get和set,而不能是readonly或writeonly。
              4. XmlAttribute,XmlAnyAttribute不能與XmlElement,XmlText,XmlAnyElement,XmlArray,XmlArrayItem一起使用。
              5. XmlRoot只能用于一個類,XmlType可用于所有類。
              6. 不同的類的XmlType不能相同,除非使用NameSpaces區分。

            相關鏈接:
              ● 在.NET中實現對象序列化
              ● 對象序列化:使用System.Xml.Serialization命名空間
              ● 對象序列化:使用XmlSerializer走完最后一步  回復  更多評論   

            # re: object 序列化 2008-03-08 12:39 愛情白面包

            不錯!!!
            頂一下!  回復  更多評論   

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804430
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久久久亚洲AV无码专区首JN| 国内精品久久久久影院薰衣草 | 久久亚洲国产最新网站| 久久久久久免费一区二区三区| 日韩人妻无码一区二区三区久久| 久久精品中文无码资源站| 亚洲人成无码www久久久| 亚洲色欲久久久久综合网 | 久久国产精品一国产精品金尊| 久久丫忘忧草产品| 亚洲αv久久久噜噜噜噜噜| 东方aⅴ免费观看久久av| 亚洲午夜久久久久妓女影院| 久久人人爽人人爽人人片av高请| 精品久久久久久久无码 | 国产精品99久久免费观看| 94久久国产乱子伦精品免费| 亚洲国产精品无码久久久秋霞2| 久久久久婷婷| 婷婷久久综合| 热re99久久6国产精品免费| 亚洲国产一成人久久精品| 97久久婷婷五月综合色d啪蜜芽| 久久久久久亚洲精品不卡| 亚洲欧美精品一区久久中文字幕 | 亚洲va中文字幕无码久久不卡 | 久久久老熟女一区二区三区| 久久精品亚洲一区二区三区浴池 | 色综合久久中文色婷婷| 91久久精品国产免费直播| 国产免费久久精品丫丫| 热99RE久久精品这里都是精品免费 | 97久久久精品综合88久久| 93精91精品国产综合久久香蕉 | 久久国产免费观看精品3| 精品人妻伦九区久久AAA片69| 久久国产色av免费看| 国产日韩欧美久久| 久久偷看各类wc女厕嘘嘘| 性做久久久久久久久老女人| 久久精品这里热有精品|