测试源代码如下,使用 VS2013 编写。
using System; using System.Collections; using System.Collections.Generic; using System.Xml.Serialization; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var f = new XmlSerializer(typeof(TestClass), new[] { typeof(DerivedClass1), typeof(ChildNS.DerivedClass1) }); var ns = new XmlSerializerNamespaces(); ns.Add("ns", "http://yourcompany.org/schemas/ns"); ns.Add("nsc", "http://yourcompany.org/schemas/ns/child"); ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); using (var sw = new StringWriter()) { f.Serialize(sw, new TestClass(), ns); Console.WriteLine(sw.ToString()); } } } public class TestClass { public BaseClass BaseTest = new BaseClass(); public BaseClass BaseTest1 = new DerivedClass1(); [XmlElement(Namespace = "http://yourcompany.org/schemas/ns/child")] public BaseClass BaseTest2 = new ChildNS.DerivedClass1(); public BaseClass[] BaseArray1 = {new BaseClass(), new DerivedClass1()}; [XmlArrayItem(typeof(BaseClass), IsNullable = true), XmlArrayItem(typeof(DerivedClass1), IsNullable = true), XmlArrayItem(typeof(DerivedClass2), IsNullable = true)] public BaseClass[] BaseArray2 = { new BaseClass(), new DerivedClass1(), null, new DerivedClass2() }; [XmlArrayItem(typeof(BaseClass), IsNullable = true)] public BaseClass[] BaseArray3 = { new BaseClass(), new DerivedClass1(), null, new DerivedClass2() }; [XmlArrayItem(typeof(BaseClass))] public List<BaseClass> BaseList = new List<BaseClass> { new BaseClass(), new DerivedClass1() }; [XmlElement(IsNullable = true)] public object NullObject1 = null; public object NullObject2 = null; //public IEnumerable BaseListEnumerable = new List<BaseClass> { new BaseClass(), new DerivedClass1() }; } public class BaseClass { [XmlAttribute("value1", Namespace = "http://yourcompany.org/schemas/ns")] public int Value1; [XmlAttribute("value2")] public int Value2; [XmlAttribute("value3", Namespace = "http://yourcompany.org/schemas/ns")] public int Value3; } public class DerivedClass1 : BaseClass { [XmlAttribute("value4", Namespace = "http://yourcompany.org/schemas/ns")] public int Value4; } public class DerivedClass2 : BaseClass { [XmlAttribute("value4", Namespace = "http://yourcompany.org/schemas/ns")] public int Value4; } namespace ChildNS { //为类型强制指定 XML 名称,以及命名空间,以避免命名冲突。 [XmlType("ChildDerivedClass", Namespace = "http://yourcompany.org/schemas/ns/child")] public class DerivedClass1 : BaseClass { public int Value5; } } }
运行结果如下
<?xml version="1.0" encoding="utf-16"?> <TestClass xmlns:ns="http://yourcompany.org/schemas/ns" xmlns:nsc="http://yourcompany.org/schemas/ns/child" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <BaseTest ns:value1="0" value2="0" ns:value3="0" /> <BaseTest1 xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> <nsc:BaseTest2 xsi:type="nsc:ChildDerivedClass" ns:value1="0" value2="0" ns:value3="0"> <nsc:Value5>0</nsc:Value5> </nsc:BaseTest2> <BaseArray1> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <BaseClass xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseArray1> <BaseArray2> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <DerivedClass1 ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> <DerivedClass2 ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseArray2> <BaseArray3> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <BaseClass xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> <BaseClass xsi:nil="true" /> <BaseClass xsi:type="DerivedClass2" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseArray3> <BaseList> <BaseClass ns:value1="0" value2="0" ns:value3="0" /> <BaseClass xsi:type="DerivedClass1" ns:value1="0" value2="0" ns:value3="0" ns:value4="0" /> </BaseList> <NullObject1 xsi:nil="true" /> </TestClass>
可以看出,
- 在 `XmlArrayItem(Attribute)`、`XmlElement` 等相关特性中指定 IsNullable = true 可以使得当前为 null / Nothing 的元素在序列化时产生一个包含 xsi:nil=”true” 特性的 XML 元素。
- 在特性中指定元素的命名空间时,应当指定命名空间的 URI,尽管可以稍后在 `XmlSerializerNamespaces` 中为这些 URI 指定对应的命名空间前缀。
- 需要序列化派生类时,总是 需要通过 XmlInclude 特性,或者通过 `XmlSerializer` 的构造函数显式声明序列化过程中可能用到的派生类。
- 数组的序列化:可以通过 `XmlArrayItem` 特性声明数组中可能包含的派生类类型。这些类型在序列化时会使用与之对应的元素名。如果存在未在 `XmlArrayItem` 中声明的派生类,而其基类在 `XmlArrayItem` 中声明过,则会使用基类对应的元素名,外加 `xsi:type` 特性声明实际派生类的类型。(关于这一点,可以参阅另一篇文章:MSDN中“(使用 XmlArrayItemAttribute 限定)序列化派生类”一节中的示例可能与实际有出入)
- 我该滚去修改以前的代码了。