Use SoapFormatter to serialize and deserialize object to xml : SoapFormatter « Network « Visual C++ .NET

Home
Visual C++ .NET
1.2D
2.Class
3.Collections
4.Data Type
5.Database ADO.net
6.Delegate
7.Development
8.File Directory
9.Function
10.Generics
11.GUI Form
12.Language Basics
13.Network
14.Reflection
15.Security
16.Statement
17.Structure
18.Thread
19.XML
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Visual C++ .NET » Network » SoapFormatter 
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();
}

   
  
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.