• <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 夢在天涯 閱讀(990) 評論(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

            搜索

            •  

            積分與排名

            • 積分 - 1804434
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久久久九国产精品| 久久精品黄AA片一区二区三区| 色综合久久无码五十路人妻| 久久一本综合| 久久露脸国产精品| 久久成人18免费网站| 国内精品伊人久久久久网站| 亚洲精品高清久久| 伊人久久综在合线亚洲2019 | 日产久久强奸免费的看| 青青青青久久精品国产| 久久99国产精品久久99果冻传媒| 久久精品www人人爽人人| 久久亚洲精品国产精品| 国产韩国精品一区二区三区久久| 国产美女久久久| 久久se这里只有精品| 伊人久久国产免费观看视频| 久久伊人五月丁香狠狠色| 精品综合久久久久久98| 大伊人青草狠狠久久| 精品久久久无码中文字幕| 久久亚洲AV成人无码| 狠狠色婷婷久久一区二区三区| 日本三级久久网| 亚洲国产小视频精品久久久三级| 亚洲国产美女精品久久久久∴| 99久久这里只有精品| 久久夜色撩人精品国产| 国内精品久久久久久99蜜桃| 久久99精品免费一区二区| 久久精品国产日本波多野结衣| 嫩草影院久久99| 久久久久亚洲av成人网人人软件| 国产精品久久久久久搜索| 亚洲国产成人久久综合碰| 国产精品久久久久久一区二区三区| 伊人 久久 精品| 国产免费久久精品99久久| 国产精品久久久久jk制服| 国内精品久久久久影院老司|