C# 序列化与反序列化之DataContract与xml对子类进行序列化的解决方案

摘要:
C#序列化和反序列化DataContract和xml序列化子类1.DataContract继承并序列化子类。第一个解决方案是在[DataContract,KnownType(typeof(继承的子类)]中添加KnownType。第二种解决方案是在序列化期间添加类型DataContractSerializercs=newDataContracts

C# 序列化与反序列化之DataContract与xml对子类进行序列化的解决方案

1、DataContract继承对子类进行序列化的解决方案

第一种是在 [DataContract, KnownType(typeof(继承的子类))]添加 KnownType(typeof(继承的子类))即可,

第二种是在序列化的时候,添加类型
DataContractSerializer dcs = new DataContractSerializer(typeof(T),new Type[] { typeof(继承的子类1), typeof(继承的子类1) })
using System.Runtime.Serialization;

namespace SupremeConsole
{
    /// <summary>
    ///  测试类,该类必未标记为可序列化,DataContractJsonSerialize,XmlSerialize可以正常序列化
    /// </summary>
    //继承TestClass的子类SubTestClass的序列化
    //[DataContract, KnownType(typeof(继承的子类)), KnownType(typeof(继承的子类1))]//KnownType指明了,继承子类的序列化的类型如 KnownType(typeof(SubTestClass)),或者再序列化的时候添加子类类型DataContractSerializer dcs = new DataContractSerializer(typeof(T),new Type[] { typeof(继承的子类1), typeof(继承的子类1) }),如:typeof(SubTestClass);
    [DataContract] //如果类型加了DataContract特性标记,而成员字段没有加DataMember特性标记的话,只有类型会序列化,成员不会序列化
    public class TestClass
    {
        /// <summary>
        /// 编号
        /// </summary>
       [DataMember] public int Id { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        [DataMember] public string Name { get; set; }

        /// <summary>
        /// 年龄
        /// </summary>
        [DataMember] public int Age { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        [DataMember] public string Address { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        [DataMember] public string Sex { get; set; }

        public override string ToString()
        {
            //return string.Format("编号:{0},姓名:{1},年龄:{2},地址:{3},性别:{4}", Id, Name, Age, Address, Sex);
            return $"编号:{Id},姓名:{Name},年龄:{Age},地址:{Address},性别:{Sex}";
        }
    }
}

继承TestClass的子类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace SupremeConsole
{
    /// <summary>
    /// 继承TestClass的子类
    /// </summary>
    [DataContract]
    public class SubTestClass : TestClass
    {
        /// <summary>
        /// 测试姓名
        /// </summary>
        [DataMember] public string SubTestClassName { get; set; }
    }
}

2、xml对继承子类进行序列化的解决方案

第一种:[XmlInclude(typeof(继承的子类1))],添加 XmlInclude(typeof(继承的子类1))即可,如 XmlInclude(typeof(XmlStudent))

第二种:序列化的时候添加子类类型XmlSerializer xs = new XmlSerializer (typeof (XmlPerson),new Type[] { typeof (继承的子类1), typeof (继承的子类2)} );,如:new Type[] { typeof (XmlStudent), typeof (XmlTeacher)}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SupremeConsole
{
    /// <summary>
    /// xml序列化测试类
    /// </summary>
    //继承XmlPerson的子类XmlStudent,XmlTeacher的序列化
    //[XmlInclude(typeof(继承的子类1))]//XmlInclude指明了,继承子类的序列化的类型,如 XmlInclude(typeof(XmlStudent)),或者再序列化的时候添加子类类型XmlSerializer xs = new XmlSerializer (typeof (XmlPerson),new Type[] { typeof (继承的子类1), typeof (继承的子类2)} );,如:new Type[] { typeof (XmlStudent), typeof (XmlTeacher)};
    [XmlRoot("haha")]
    //[XmlInclude(typeof(XmlStudent))]
    //[XmlInclude(typeof(XmlTeacher))]
    public class XmlPerson
    {
        /// <summary>
        /// 姓名
        /// </summary>       
        [XmlElement("MyName", Order = 2)]
        public string Name { get; set; }

        /// <summary>
        /// 年龄
        /// </summary>       
        [XmlAttribute("MyAge")]
        public int Age { get; set; }

        /// <summary>
        /// 住址
        /// </summary>
        [XmlElement("Residence", Order = 1)]
        public string Address { get; set; }
    }
    [XmlType("SubXmlPersonIsXmlStudent")]//XmlStudent序列化后的名称
    public class XmlStudent : XmlPerson
    {
        /// <summary>
        /// 学号
        /// </summary>
        public string StuNumber { get; set; }
    }

    [XmlType("SubXmlPersonIsXmlTeacher")]//XmlTeacher序列化后的名称
    public class XmlTeacher : XmlPerson
    {
        /// <summary>
        /// 工号
        /// </summary>
        public string TeachNumber { get; set; }

    }
C# 序列化与反序列化之DataContract与xml对子类进行序列化的解决方案,就是利用特性和序列化的时候指明类型即可

总结:
DataContract子类序列化:特性[DataContract, KnownType(typeof(继承的子类)), KnownType(typeof(继承的子类1))]//KnownType指明了,继承子类的序列化的类型如 KnownType(typeof(SubTestClass)),或者再序列化的时候添加子类类型DataContractSerializer dcs = new DataContractSerializer(typeof(T),new Type[] { typeof(继承的子类1), typeof(继承的子类1) }),如:typeof(SubTestClass);
xml子类序列化:特性[XmlInclude(typeof(继承的子类1))]//XmlInclude指明了,继承子类的序列化的类型,如 XmlInclude(typeof(XmlStudent)),或者再序列化的时候添加子类类型XmlSerializer xs = new XmlSerializer (typeof (XmlPerson),new Type[] { typeof (继承的子类1), typeof (继承的子类2)} );,如:new Type[] { typeof (XmlStudent), typeof (XmlTeacher)};

测试代码:
using log4net;
using System;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.IO.MemoryMappedFiles;
using System.IO.Pipes;
using System.Linq;
using System.Net;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;

namespace SupremeConsole
{
    class Program
    {
       
        static void Main(string[] args)
        {
            
            TestSeri();
            Console.ReadLine();
        }

       
        public static void TestSeri()
        {
            //Team team = new Team { TName="123",PlayerList = { new Person { Name="1",Age=1},new Person { Name = "2", Age = 2 } } };
            #region BinarySerialize 必须添可序列化属性,即要序列化的对象必须添加SerializableAttribute属性,[Serializable]
            //string s = SerializeManager.Instance.BinarySerialize<Team>(team);//序列化
            //Console.ForegroundColor = ConsoleColor.Green;
            //Console.WriteLine("测试序列化成功。。。");
            //Console.WriteLine($"测试序列化结果:
{s}");

            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "序列化11111.bin");//序列化
            //SerializeManager.Instance.BinarySerialize<Team>(team, path);

            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "序列化11111.bin");
            //Team test = SerializeManager.Instance.BinaryDeserialize<Team>(path);//反序列化
            //if (test != null)
            //{
            //    Console.WriteLine($"测试序列化结果:{test.ToString()}");
            //}
            #endregion

            #region SoapSerialize 必须添可序列化属性,即要序列化的对象必须添加SerializableAttribute属性,[Serializable]
            //string s = SerializeManager.Instance.SoapSerialize<Team>(team);//序列化
            //Console.ForegroundColor = ConsoleColor.Green;
            //Console.WriteLine("测试序列化成功。。。");
            //Console.WriteLine($"测试序列化结果:
{s}");

            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Soap序列化.xml");//序列化
            //SerializeManager.Instance.SoapSerialize<Team>(team, path);


            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Soap序列化.xml");
            //Team test = SerializeManager.Instance.SoapDeserialize<Team>(path);//反序列化
            //if (test != null)
            //{
            //    Console.WriteLine($"测试序列化结果:{test.ToString()}");
            //}
            #endregion

            #region XmlSerialize 要序列化的对象可以不添加SerializableAttribute属性,[Serializable]
            XmlPerson xmlPerson = new XmlPerson { Name = "1111", Age = 12,Address="住址。。。。" };
            //XmlPerson xmlPerson = new XmlPerson { Name = "1111", Age = 12, HomeAddress = new USAddress { Street = "默默大街三号", PostCode = "233664", Neighbor="邻居666" } };
            string s = SerializeManager.Instance.XmlSerialize<XmlPerson>(xmlPerson);//序列化
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("测试序列化成功。。。");
            Console.WriteLine($"测试序列化结果:
{s}");

            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "序列化.txt");//序列化
            //SerializeManager.Instance.XmlSerialize<TestClass>(testClass, path);

            //备用
            //string json = "{"Address":"中国南京","Age":10,"Id":1,"Name":"张三","Sex":"男"}";
            //TestClass test = SerializeManager.Instance.DataContractJsonDeserializeJson<TestClass>(json);//反序列化
            //if (test != null)
            //{
            //    Console.WriteLine($"测试序列化结果:{test.ToString()}");
            //}

            //string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "序列化.txt");
            //TestClass test = SerializeManager.Instance.XmlDeserialize<TestClass>(path);//反序列化
            //if (test != null)
            //{
            //    Console.WriteLine($"测试序列化结果:{test.ToString()}");
            //} 
            #endregion
        }
} }

免责声明:文章转载自《C# 序列化与反序列化之DataContract与xml对子类进行序列化的解决方案》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇BCM94352HMB蓝牙BCM20702A0在Ubuntu 14.04下的驱动方法Xcode版本对应的iOS SDK和Mac OS下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Delphi操作Excel大全

转自上帝的鱼--专栏 cdsn 个人收藏:Delphi 控制Excel(一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObject( 'Excel.Application' );1) 显示当前窗口:ExcelApp.Visible := True;...

springboot配置rabbitmq的序列化反序列化格式

SpringBoot封装了rabbitmq中,发送对象和接收对象时,会统一将对象和消息互相转换 会用到MessageConverter转换接口 在发送消息时, 会将Object转换成Message Message createMessage(Object object, MessageProperties messageProperties) 接收消息...

XML Serializable Generic Dictionary

.net 2.0 泛型Dictionary不支持 XML serializable. 下面是一个实现IXmlSerializable 接口实现支持Serialize的泛型集合.Dictionary。 Dictionary<TKey, TValue>本身实现了ISerializable接口,WebService中无法实现序列化,具体是什么原因它不...

【操作系统之十四】iptables扩展模块

1、iprange 使用iprange扩展模块可以指定"一段连续的IP地址范围",用于匹配报文的源地址或者目标地址。--src-range:匹配报文的源地址所在范围--dst-range:匹配报文的目标地址所在范围eg:iptables -t filter -I INPUT -m iprange --src-range 192.168.1.15-192.1...

DRF 序列化组件

Serializers 序列化组件 Django的序列化方法 classBooksView(View): defget(self, request): book_list = Book.objects.values("id", "title", "chapter", "pub_time", "publisher")...

Json.Net序列化和反序列化设置

首先补充一点,Json.Net是支持序列化和反序列化DataTable,DataSet,Entity Framework和NHibernate的.我举例说明DataTable的序列化和反序列化.创建一个DataTable对象,如下: DataTable dt = newDataTable(); DataCol...