首先引用msdn上的內容,是關於XmlSerializer的構造函數(Type, Type[])的:
默認情況下,如果某公共屬性或字段返回對象或對象數組,則將自動序列化此對象類型。但是,如果某個類包含返回Object類型的數組的字段或屬性,則可以將任何對象插入此數組。在此情況下,必須指示 XmlSerializer,請求將要插入到 Object 數組的所有可能的對象類型。若要執行該操作,請使用 extraTypes 參數指定要序列化或反序列化的其他對象類型。
還可以使用 extraTypes 參數指定從基類派生的類型。例如,假設有一個名為 Phone 的基類,並且一個名為 InternationalPhone 的類從該基類派生。同時,使用 extraTypes參數來指定派生類型。
第一種使用的情況:
這裡給出測試代碼:
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(); } } }
UserInfo info = new UserInfo() { userId = 1, userName = Guozhijian, a = new B() };
將Main函數修改如下:
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(); }
第二種使用的情況:
這裡給出測試代碼:
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(); } } }
將Main函數修改如下:
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(); }