C# 序列化与反序列化之xml通过实现IXmlSerializable进行序列化的解决方案

2021-01-25 00:16

阅读:384

标签:ati   成功   mem   解决   项目   art   reg   gen   新建   

新建控制台console项目,添加XmlPersonIXmlSerializable类,和AddressIXmlSerializable类(实现IXmlSerializable)以及AddressIXmlSerializable子类USAddressIXmlSerializable

代码如下:

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

namespace SupremeConsole
{
    #region 测试IXmlSerializable
    public class XmlPersonIXmlSerializable
    {
        /// 
        /// 姓名
        ///  
        public string Name { get; set; }

        /// 
        /// 住址
        /// 
        public AddressIXmlSerializable HomeAddress = new USAddressIXmlSerializable();//子类的序列化

    }

    public class AddressIXmlSerializable : IXmlSerializable
    {
        public string Street, PostCode;

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            reader.ReadStartElement();
            Street = reader.ReadElementContentAsString("Street", null);
            PostCode = reader.ReadElementContentAsString("PostCode", null);
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteElementString("Street", Street);
            writer.WriteElementString("PostCode", PostCode);
        }
    }

    public class USAddressIXmlSerializable : AddressIXmlSerializable
    {
        public string Neighbor;
    }
    #endregion
}

 

主方法代码:

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;
using System.Collections.Generic;

namespace SupremeConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlPersonIXmlSerializable xmlPersonIXmlSerializable = new XmlPersonIXmlSerializable { Name = "11111", HomeAddress = new AddressIXmlSerializable { Street = "大街三三号", PostCode = "123" } };
            string s = SerializeManager.Instance.XmlSerialize(xmlPersonIXmlSerializable);//序列化
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("测试序列化成功。。。");
            Console.WriteLine($"测试序列化结果:\r\n{s}");
            Console.ReadLine();
        } 
  }
}

 

运行结果:

技术图片

 

C# 序列化与反序列化之xml通过实现IXmlSerializable进行序列化的解决方案

标签:ati   成功   mem   解决   项目   art   reg   gen   新建   

原文地址:https://www.cnblogs.com/1175429393wljblog/p/12036441.html


评论


亲,登录后才可以留言!