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 夢在天涯 閱讀(1008) 評論(3) 編輯 收藏 引用 所屬分類: C#/.NET

