Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Use SoapFormatter to serialize and deserialize object to xml
#include "stdafx.h"
#using <system.runtime.serialization.formatters.soap.dll>
using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Soap;
[Serializable]
ref class RoleAttr
{
public:
property int Charisma;
RoleAttr(int Cha);
void Print();
};
RoleAttr::RoleAttr(int Cha)
{
this->Charisma = Cha;
}
void RoleAttr::Print()
{
Console::WriteLine(Charisma);
}
[Serializable]
ref class Role
{
public:
property String ^Name;
property String ^Race;
property String ^Class;
property RoleAttr ^pattr;
Role (String ^Name, String ^Race, String ^Class,int Cha);
void Print();
};
Role::Role (String ^Name, String ^Race, String ^Class,int Cha)
{
this->Name = Name;
this->Race = Race;
this->Class = Class;
this->pattr = gcnew RoleAttr(Cha);
}
void Role::Print()
{
Console::WriteLine("Name: {0}", Name);
Console::WriteLine("Race: {0}", Race);
Console::WriteLine("Class: {0}", Class);
pattr->Print();
}
int main(void){
Role ^r = gcnew Role("r", "H", "Thief", 11);
r->Print();
FileStream ^plStream = File::Create("Role.xml");
SoapFormatter ^sf = gcnew SoapFormatter();
sf->Serialize(plStream, r);
plStream->Close();
plStream = File::OpenRead("Role.xml");
Role ^rClone = (Role^)sf->Deserialize(plStream);
plStream->Close();
Console::WriteLine("\nCloned r");
rClone->Print();
}