SoFunction
Updated on 2025-03-07

Pay attention to details when serializing C# into XML

The most commonly used serialization is to serialize a class into a binary file. But sometimes we also serialize the class into an xml file.
If there is a class below
Copy the codeThe code is as follows:

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 above class Arwen into binary, there is no problem at all. Just add [Serializable] in front of it. In addition, if there are fields or attributes in the class, you should also add [Serializable] in front of the corresponding class definition of that class. If a field or attribute in the class does not want to be serialized, just add [NonSerialized] in front of it. Serializing it into binary is equivalent to saving all information into a binary file intact. Whether it is a private field or public, or whatever type it is
Serializing into XML is subject to many restrictions than serializing into binary, with three main ones.
1 is that only public fields or attributes can be serialized.
2. Some types cannot be serialized. For example, the above types such as Hastable and TimeSpan.
3. A constructor with no parameters must be serialized into xml in the class.
So what if you really want to save information like Hastable and TimeSpan?

Then you can only save the country in a curve. Do a conversion in the middle, first convert Hastable and Timespan to other types. Then serialize it into xml, and then convert it when deserializing it back. That also means you have to add a certain type to the original class. For example, you have a new class pair, which has two fields, corresponding to the Hastable key-value pair, and then use a List<pairs> to save all the information in the Hastable. At this time, List<pairs> can be serialized. This is obviously a very troublesome and stupid way. But it seems that there is no better way. TimeSpan can be converted to string type first.

The following is just a simple usage of xml conversion. Types such as Hashtable mentioned above cannot be serialized, so you can use a feature to explicitly declare it as not serialized. Use [XmlIgnoreAttribute]
Copy the codeThe code is as follows:

using ;
using ;
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();
= new TimeSpan(1,2,3); // 1 hours , 2 minutes, 3 seconds
("arwen", 250);
string filename = @"c:\temp\";
XmlSerializer xs = new XmlSerializer(typeof(Arwen));
using (FileStream file = new FileStream(filename, ))
(file, sa);