C# .Net 中序列化和反序列化的简单应用

摘要:
关键词:
None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Collections;
None.gif
using  System.Runtime.Serialization.Formatters.Binary;
None.gif
using  System.Runtime.Serialization;
None.gif
None.gif
public   class  App 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [STAThread]
InBlock.gif    
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Serialize();
InBlock.gif        Deserialize();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static void Serialize() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        ArrayList TestList 
= new ArrayList();
InBlock.gif        
for(int i = 1;i<=3;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TestNode testNode 
= new TestNode(DateTime.Now.ToString(),i.ToString());
InBlock.gif            testNode.TestSubNode.Name 
= "Sub"+i.ToString();
InBlock.gif            testNode.TestSubNode.Number 
= "Sub"+DateTime.Now.ToString();
InBlock.gif            TestList.Add(testNode);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// To serialize the ArrayList and its key/value pairs,  
InBlock.gif        
// you must first open a stream for writing. 
InBlock.gif        
// In this case, use a file stream.
InBlock.gif
        FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
InBlock.gif
InBlock.gif        
// Construct a BinaryFormatter and use it to serialize the data to the stream.
InBlock.gif
        BinaryFormatter formatter = new BinaryFormatter();
InBlock.gif        
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
//            formatter.Serialize(fs, addresses);
InBlock.gif
            formatter.Serialize(fs, TestList);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (SerializationException e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Failed to serialize. Reason: " + e.Message);
InBlock.gif            
throw;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fs.Close();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif   
InBlock.gif    
static void Deserialize() 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif          ArrayList TestList 
= new ArrayList();
InBlock.gif        
// Open the file containing the data that you want to deserialize.
InBlock.gif
        FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
InBlock.gif        
try 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            BinaryFormatter formatter 
= new BinaryFormatter();
InBlock.gif
InBlock.gif            
// Deserialize the ArrayList from the file and 
InBlock.gif            
// assign the reference to the local variable.
InBlock.gif            
//addresses = (ArrayList) formatter.Deserialize(fs);
InBlock.gif
            TestList = (ArrayList)formatter.Deserialize(fs);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (SerializationException e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Failed to deserialize. Reason: " + e.Message);
InBlock.gif            
throw;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            fs.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// To prove that the table deserialized correctly, 
InBlock.gif        
// display the key/value pairs.
InBlock.gif
        foreach (TestNode de in TestList) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"TestNode({0}, {1}).\nTestSubNode({2},{3})", de.Number, de.Name,de.TestSubNode.Number,de.TestSubNode.Name);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif[Serializable]
None.gif
public   class  TestNode
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private TestSubNode m_TestSubNode = new TestSubNode("","");
InBlock.gif    
private string m_Number;
InBlock.gif    
private string m_Name;
InBlock.gif    
public TestNode(string number,string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m_Number 
=number;
InBlock.gif        m_Name 
= name;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Number
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
setdot.gif{m_Number=value;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gif{return m_Number;}
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
setdot.gif{m_Name=value;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gif{return m_Name;}
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public void SetTestSubNode(string number,string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m_TestSubNode.Number 
= number;
InBlock.gif        m_TestSubNode.Name 
= name;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public TestSubNode TestSubNode
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
setdot.gif{m_TestSubNode=value;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gif{return m_TestSubNode;}
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif[Serializable]
None.gif
public   class  TestSubNode
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
string m_Number;
InBlock.gif    
string m_Name;
InBlock.gif    
public TestSubNode(string number,string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m_Number 
=number;
InBlock.gif        m_Name 
= name;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Number
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
setdot.gif{m_Number=value;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gif{return m_Number;}
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
setdot.gif{m_Name=value;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
getdot.gif{return m_Name;}
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/MichaelLu/archive/2006/03/04/342660.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值