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


    新建控制台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
        {
            /// <summary>
            /// 姓名
            /// </summary> 
            public string Name { get; set; }
    
            /// <summary>
            /// 住址
            /// </summary>
            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>(xmlPersonIXmlSerializable);//序列化
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("测试序列化成功。。。");
                Console.WriteLine($"测试序列化结果:
    {s}");
                Console.ReadLine();
            } 
      }
    }

    运行结果:

  • 相关阅读:
    【构建二叉树】01根据前序和中序序列构造二叉树【Construct Binary Tree from Preorder and Inorder Traversal】
    PHP 语言需要避免的 10 大误区
    极客编程必备的五大PHP开发应用
    你听说过PHP 的面向方面编程吗?
    8个开发必备的PHP功能
    写给系统管理员的25个PHP安全实践
    PHP输出缓冲控制- Output Control 函数应用详解
    创建高安全性PHP网站的几个实用要点
    简化PHP开发的10个工具
    PHP文件下载原理
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/12036441.html
Copyright © 2020-2023  润新知