XmlSerialization [英] XmlSerialization

查看:39
本文介绍了XmlSerialization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何序列化静态变量?

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...

推荐答案

最简单的方法是创建另一个类,也许称为AddressInstance.默认构造函数仅从Address类的静态变量获取值,并将它们分配给该类的实例变量.

要么,要么您可以编写自定义序列化器.
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.


ki重现代码:

公共引用类Address1
{
公众:
静态数组< String ^> ^ Name = gcnew数组< String ^> (2);
静态字符串^ Line1;
静态字符串^城市;
静态字符串^状态; <​​br/> 静态字符串^ Zip;
};

公共引用类地址
{
公众:
数组< String ^> ^名称;
字符串^ Line1;
字符串^城市;
字符串^状态; <​​br/> 字符串^邮编;
地址()
{Name = gcnew数组< String ^>(2);}

};

void CreatePO(String ^文件名)
{

XmlSerializer ^序列化器= gcnew XmlSerializer(地址:: typeid);
TextWriter ^ writer = gcnew StreamWriter(filename);
地址^ billAddress = gcnew地址;

billAddress->名称[0] =地址1 ::名称[0];
billAddress-> Name [1] = Address1 :: Name [1]
billAddress-> Line1 = Address1 :: Line1;
billAddress-> City =地址1 :: City;
billAddress->状态=地址1 ::状态;
billAddress-> Zip =地址1 :: Zip;

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

我的输出xml如下,我该如何将childnodes的名称< string>更改为我想要的名称?
<?xml version ="1.0" encoding ="utf-8"吗?>
-<地址xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema">
-<名称>
< string> aaaaaaa</string>
< string> qqqqqqq</string>
</名称>
< Line1> klka</Line1>
< City>新加坡< City>
< State> idjd</State>
< Zip> jjjj</Zip>
</地址>
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>


这篇关于XmlSerialization的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆