C # precautions for serialization into XML

Source: Internet
Author: User

The most common serialization is to serialize a class into a binary file, but sometimes we will serialize the class into an XML file.

For example

Class Arwen

{

Private hashtable table = new hashtable ();

Private timespan time = new timespan (0, 0, 1 );

Public hashtable table

{

Get {return table ;}

Set {table = value ;}

}

Public timespan time

{

Get {return time ;}

Set {time = value ;}

}

 

Public string name {Get; set ;}

}

 

If you serialize the Arwen class to binary, there is no problem at all. just add a [serializable] in front of it. in addition, if a field or attribute is a class in the class, you must add [serializable] before the class definition. if a field or attribute in the class does not want to be serialized, simply add [nonserialized] before it. serialization to binary is equivalent to saving all information to binary files. private or public. no matter what type

 

Serialization to XML is much more restrictive than serialization to binary, mainly including three.

1. Only public fields or attributes can be serialized.

2. Some types cannot be serialized, such as the preceding hastable and timespan types.

3. constructor with or without parameters in the class can be serialized into XML.

 

 

What if you really want to save information of the hastable and timespan types?

That's the only way to save the country. convert the hastable and timespan to another type. it is serialized into XML and then converted when deserialization is returned. that also means you need to add a type to the original class. for example, a new pairs class has two fields corresponding to the hastable key-value pair, and then stores all the information in hastable using a list <pairs>. in this case, list <pairs> can be serialized. this is obviously a very troublesome and stupid method. but it seems that there is no better way. timespan can be converted to the string type first.

 

The following describes the simple usage of XML Conversion. As mentioned above, hashtable and other types cannot be serialized. You can use a feature to explicitly declare them as not serializable. Use [xmlignoreattribute ].

 

Using system. xml. serialization;

Using system. IO;

 

Class Arwen

{

Private hashtable table = new hashtable ();

Private timespan time = new timespan (0, 0, 1 );

Public Arwen ()

{

}

[Xmlignoreattribute]

Public hashtable table

{

Get {return table ;}

Set {table = value ;}

}

[Xmlignoreattribute]

Public timespan time

{

Get {return time ;}

Set {time = value ;}

}

}

 

Arwen weiwen = new Arwen ();

Weiwen. Time = new timespan (1, 2, 3); // 1 hours, 2 minutes, 3 seconds

Weiwen. Table. Add ("Arwen", 250 );

 

String filename = @ "C: \ temp \ Arwen. xml ";

Xmlserializer xs = new xmlserializer (typeof (Arwen ));

Using (filestream file = new filestream (filename, filemode. Create ))

Xs. serialize (file, SA );

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.