[C # Basics] about when to use the XmlSerializer Constructor (Type, Type [])

Source: Internet
Author: User

[C # Basics] about when to use the XmlSerializer Constructor (Type, Type [])

First, reference the content on msdn, which is about the XmlSerializer Constructor (Type, Type:

 

 

By default, if a public property or field returns an object or an object array, the object type is automatically serialized. However, if a class contains fields or attributes of an array of the Object type returned, any Object can be inserted into this array. In this case, the XmlSerializer must be specified to request all possible Object types to be inserted into the Object array. To perform this operation, use the extraTypes parameter to specify other object types to be serialized or deserialized.

You can also use the extraTypes parameter to specify the type derived from the base class. For example, assume that there is a base class named Phone and a class named InternationalPhone is derived from this base class. In addition, the extraTypes parameter is used to specify the derived type.

 

The first use case:

The test code is provided here:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{    public class A     {        public string x = aaa;    }    public class B : A    {        public int i = 1;    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Serialization;namespace ConsoleApplication1{    public class UserInfo    {        public int userId;        public string userName;        public A a;    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Xml;using System.Xml.Serialization;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            UserInfo info = new UserInfo() { userId = 1, userName = Guozhijian, a = new A() };            string s;            using (StringWriter sw = new StringWriter())            {                XmlSerializer xz = new XmlSerializer(info.GetType());                xz.Serialize(sw, info);                s = sw.ToString();            }            Console.WriteLine(s);            Console.ReadLine();        }    }}

The above code can be successfully run, but if you change the first sentence of the Main function:

 

 

UserInfo info = new UserInfo() { userId = 1, userName = Guozhijian, a = new B() };

An error is reported during the runtime because the UserInfo class contains a base class that is converted to a derived class. Then, the main character is displayed.

 

Modify the Main function as follows:

 

static void Main(string[] args)        {            Type[] types = new Type[] { typeof(B)};            UserInfo info = new UserInfo() { userId = 1, userName = Guozhijian, a = new B() };            string s;            using (StringWriter sw = new StringWriter())            {                XmlSerializer xz = new XmlSerializer(info.GetType(),types);                xz.Serialize(sw, info);                s = sw.ToString();            }            Console.WriteLine(s);            Console.ReadLine();        }

In this way, you can run it again! That is to say, if the class to be serialized contains a base class to be converted to a derived class, you can create a Type array, give it the derived class you need, and then use (Type, type []) constructor.

 

 

The second use case:

The test code is provided here:

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Serialization;namespace ConsoleApplication1{    public class UserInfo    {        public int userId;        public string userName;        public Object[] a;    }}

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Xml;using System.Xml.Serialization;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            A a1 = new A();            a1.x = a1;            A a2 = new A();            a2.x = a2;            A[] aArray = new A[] { a1, a2 };            UserInfo info = new UserInfo() { userId = 1, userName = Guozhijian, a = aArray };            string s;            using (StringWriter sw = new StringWriter())            {                XmlSerializer xz = new XmlSerializer(info.GetType());                xz.Serialize(sw, info);                s = sw.ToString();            }            Console.WriteLine(s);            Console.ReadLine();        }    }}

This cannot be run, because the UserInfo class contains the fields returned from the array of the Object type, and we assigned the array of A class to this field. Then our leading role is playing again ..

 

Modify the Main function as follows:

 

static void Main(string[] args)        {            Type[] types = new Type[] { typeof(A)};            A a1 = new A();            a1.x = a1;            A a2 = new A();            a2.x = a2;            A[] aArray = new A[] { a1, a2 };            UserInfo info = new UserInfo() { userId = 1, userName = Guozhijian, a = aArray };            string s;            using (StringWriter sw = new StringWriter())            {                XmlSerializer xz = new XmlSerializer(info.GetType(),types);                xz.Serialize(sw, info);                s = sw.ToString();            }            Console.WriteLine(s);            Console.ReadLine();        }

Now our code can run again! That is to say, if the class we want to serialize contains the field or attribute of the array that returns the Object Type, and this field or attribute is assigned a value of another Type, you can create a Type array, give it other types, and then use the (Type, Type []) constructor.

 

 


 

Related Article

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.