Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i serialize static variable??
public ref class Address
{
public:
    static String^ Name;
    static String^ Line1;
    static String^ City;
    static String^ State;
    static String^ Zip;
};


Here is my write function
void CreatePO( String^ filename )
{

   XmlSerializer^ serializer = gcnew XmlSerializer( Address::typeid );
   TextWriter^ writer = gcnew StreamWriter( filename );
   Address^ billAddress = gcnew Address;


   billAddress->Name ="jjjjjj" ;
   billAddress->Line1 = "1 Main St.";
   billAddress->City = "AnyTown";
   billAddress->State = "WA";
   billAddress->Zip = "00000";


   serializer->Serialize( writer, billAddress );
   writer->Close();
}


How can i modify it so it can save a static variable also...
Posted

The easiest way would be to create another class, perhaps called AddressInstance. The default constructor would just get the values from the static variables on the Address class and they would be assigned to instance variables on the class.

Either that, or you can write a custom serializer.
 
Share this answer
 
k i reproduce the code:

public ref class Address1
{
public:
static array<String^> ^Name=gcnew array<String^> (2);
static String^ Line1;
static String^ City;
static String^ State;
static String^ Zip;
};

public ref class Address
{
public:
array <String^> ^Name;
String^ Line1;
String^ City;
String^ State;
String^ Zip;
Address()
{Name=gcnew array<String^>(2);}

};

void CreatePO( String^ filename )
{

XmlSerializer^ serializer = gcnew XmlSerializer( Address::typeid );
TextWriter^ writer = gcnew StreamWriter( filename );
Address^ billAddress = gcnew Address;

billAddress->Name[0]=Address1::Name[0];
billAddress->Name[1]=Address1::Name[1]
billAddress->Line1 = Address1::Line1 ;
billAddress->City = Address1::City ;
billAddress->State = Address1::State ;
billAddress->Zip = Address1::Zip ;

.
serializer->Serialize( writer, billAddress );
writer->Close();
}

my output xml is as follow,how can i change the childnodes'name <string>to what i want?i want the aaaaaa inside a childnode name param1 and qqqqqqq inside childnode name param2.

<?xml version="1.0" encoding="utf-8" ?>
- <Address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Name>
<string>aaaaaaa</string>
<string>qqqqqqq</string>
</Name>
<Line1>klka</Line1>
<City>singapore</City>
<State>idjd</State>
<Zip>jjjj</Zip>
</Address>
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900