Doubts DataContractSerializer sequence data contract (1)

 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:

 

 

 1  [DataContract]  
 2   public   class  Person  
 3 ExpandedBlockStart.gifContractedBlock.gif  {  
 4     [DataMember]  
 5     public string ID;  
 6     [DataMember]  
 7     public string FirstName;  
 8     [DataMember]  
 9     public string LastName;  
10 }
 

 

Test Case 1:
 1 [Test]
 2          public   void  Test1()
 3 ExpandedBlockStart.gifContractedBlock.gif         {
 4            List<Person> list = new List<Person>();
 5
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            list.Add(new Person { ID = "1", FirstName = "Lee", LastName = "Jetson" });
 7ExpandedSubBlockStart.gifContractedSubBlock.gif            list.Add(new Person { ID = "1", FirstName = "Chen", LastName = "Mc" });
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            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)
19ExpandedSubBlockStart.gifContractedSubBlock.gif            {
20                Console.WriteLine(node.InnerText);
21            }

22            Console.WriteLine(xml);
23        }
 
This is a time ah very simple code, the sequence of results is printed out on the console:
 1 < ArrayOfPerson  xmlns ="http://schemas.datacontract.org/2004/07/Lzx.Test.Xml"  xmlns:i ="http://www.w3.org/2001/XMLSchema-instance" >
 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:

 1 [Test]
 2          public   void  Test2()
 3 ExpandedBlockStart.gifContractedBlock.gif         {
 4            List<Person> list = new List<Person>();
 5
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            list.Add(new Person { ID = "1", FirstName = "Lee", LastName = "Jetson" });
 7ExpandedSubBlockStart.gifContractedSubBlock.gif            list.Add(new Person { ID = "1", FirstName = "Chen", LastName = "Mc" });
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            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

 

在控制台打印结果如下:

 

 1 <? xml version="1.0" ?>
 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

Guess you like

Origin blog.csdn.net/weixin_33890499/article/details/94066028