Preface:
By comparing article is purely for DataContractSerializer and XmlSerializer rather point out some of the details in DataContractSerializer serialized in. If the errors and omissions in the presence of welcome that.
I will be in three space in the form of code as we show experience.
In the MCF, DataContractSerializer default sequencer. DataContractSerializer use of relatively simple, WriteObject () and the ReadObject () is its two main methods.
But in practice, I found that there is little doubt serialized result of DataContractSerializer.
First Person define the object of a test:
2 public class Person
3 {
4 [DataMember]
5 public string ID;
6 [DataMember]
7 public string FirstName;
8 [DataMember]
9 public string LastName;
10 }
2 public void Test1()
3 {
4 List<Person> list = new List<Person>();
5
6 list.Add(new Person { ID = "1", FirstName = "Lee", LastName = "Jetson" });
7 list.Add(new Person { ID = "1", FirstName = "Chen", LastName = "Mc" });
8 list.Add(new Person { ID = "1", FirstName = "He", LastName = "James" });
9
10 DataContractSerializer ser = new DataContractSerializer(typeof(List<Person>));
11 MemoryStream ms = new MemoryStream();
12 ser.WriteObject(ms, list);
13 string xml = Encoding.UTF8.GetString(ms.ToArray());
14
15 XmlDocument doc = new XmlDocument();
16 doc.LoadXml(xml);
17
18 foreach (XmlNode node in doc.ChildNodes)
19 {
20 Console.WriteLine(node.InnerText);
21 }
22 Console.WriteLine(xml);
23 }
2 < Person >
3 < FirstName > Lee </ FirstName >
4 < ID > 1 </ ID >
5 < LastName > Jetson </ LastName >
6 </ Person >
7 < Person >
8 < FirstName > Chen </ FirstName >
9 < ID > 1 </ ID >
10 < LastName > Mc </ LastName >
11 </ Person >
12 < Person >
13 < FirstName > He </ FirstName >
14 < ID > 1 </ ID >
15 < LastName > James </ LastName >
16 </ Person >
17 </ ArrayOfPerson >
18
This is in line with xml format.
Also see another period of testing, a very simple direct DataContractSerializer replaced XmlSerializer:
2 public void Test2()
3 {
4 List<Person> list = new List<Person>();
5
6 list.Add(new Person { ID = "1", FirstName = "Lee", LastName = "Jetson" });
7 list.Add(new Person { ID = "1", FirstName = "Chen", LastName = "Mc" });
8 list.Add(new Person { ID = "1", FirstName = "He", LastName = "James" });
9
10 XmlSerializer ser = new XmlSerializer(typeof(List<Person>));
11 MemoryStream ms = new MemoryStream();
12 ser.Serialize(ms, list);
13 string xml = Encoding.UTF8.GetString(ms.ToArray());
14 Console.WriteLine(xml);
15 }
16
在控制台打印结果如下:
2 < ArrayOfPerson xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
3 < Person >
4 < ID > 1 </ ID >
5 < FirstName > Lee </ FirstName >
6 < LastName > Jetson </ LastName >
7 </ Person >
8 < Person >
9 < ID > 1 </ ID >
10 < FirstName > Chen </ FirstName >
11 < LastName > Mc </ LastName >
12 </ Person >
13 < Person >
14 < ID > 1 </ ID >
15 < FirstName > He </ FirstName >
16 < LastName > James </ LastName >
17 </ Person >
18 </ ArrayOfPerson >
Two sequences of the results through careful comparison will find that there are a few differences:
First, xml file header. Using the data contract is not <? Xml version = "1.0" encoding = "utf-8"?> This tag.
Second, the namespace. The two default xml namespace are very different.
Third, formatting. Data output is the result of contract unformatted string of string (I posted the results of a hand-formatted for easy reading.), And xml serialization a result has been formatted xml.
Reproduced in: https: //www.cnblogs.com/xinggg22/archive/2009/11/26/1611004.html