• C#实现接口xml序列化与反序列化


    C#实现接口xml序列化与反序列化

     

    C#中接口无法被xml序列化,提示不支持。百度和bing也搜不到,只好自己动手写了

    原理上肯定支持,.Net自己的xml序列化有一个IXmlSerializable接口。

    自行实现该接口,并取得类名,再进行自定义的序列化处理即可。

    复制代码
    using System.Collections;
    using System.Xml.Serialization;
    using System;
    using System.Xml;
    
    [XmlRoot("SerInterface"), Serializable]
    public class SerInterface<TInterface> : IXmlSerializable
    {
        public TInterface Source { get { return (TInterface)mSource; } }
        string mTypeName;
        object mSource;
    
    
        public SerInterface()
        {
    
        }
    
        public SerInterface(object interfaceObj)
        {
            this.mSource = interfaceObj;
            mTypeName = mSource.GetType().FullName;
        }
    
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }
    
        public void ReadXml(XmlReader reader)
        {
            var typeSer = new XmlSerializer(typeof(string));
            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();
    
            if (wasEmpty)
                return;
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                reader.ReadStartElement("Type");
                mTypeName = typeSer.Deserialize(reader) as string;
    
                reader.ReadEndElement();
    
                var sourceSer = new XmlSerializer(Type.GetType(mTypeName));
                reader.ReadStartElement("Object");
                mSource = sourceSer.Deserialize(reader);
                reader.ReadEndElement();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }
    
        public void WriteXml(XmlWriter writer)
        {
            var typeSer = new XmlSerializer(typeof(string));
            var sourceSer = new XmlSerializer(mSource.GetType());
    
            writer.WriteStartElement("Type");
            typeSer.Serialize(writer, mTypeName);
            writer.WriteEndElement();
            writer.WriteStartElement("Object");
            sourceSer.Serialize(writer, mSource);
            writer.WriteEndElement();
        }
    }
    复制代码

    使用:

    复制代码
    //把接口嵌套
    IXXX tmp = new Class1();
    var serInterface = new SerInterface<IXXX>(tmp);
    
    
    //序列化
    var xmlSer = new XmlSerializer(serInterface.GetType());
    var writer = new MemoryStream();
    
    xmlSer.Serialize(writer, serInterface);
    
    StreamReader sr = new StreamReader(writer);
    var serStr = Encoding.UTF8.GetString(writer.ToArray());
    
    writer.Flush();
    writer.Close();
    sr.Close();
    
    
    //反序列化
    xmlSer = new XmlSerializer(typeof(SerInterface<IXXX>));
    TextReader reader = new StringReader(serStr);
    var deSer =  xmlSer.Deserialize(reader);
    
    //原始数据
    var sourceInterfaceData = (deSer as SerInterface<IXXX>).Source;
    复制代码
     
    分类: C# & .Net
  • 相关阅读:
    在oschina上新建项目的步骤
    将txt转为DataTable的方法
    设置IIS让网站拥有“网站目录外文件”的读写权限的操作(图文)
    从客户端****中检测到有潜在危险的 Request.QueryString 值在.net mvc下的解决方法
    动态调用类里的方法的示例(wjx)
    Pyhton忽略返回变量方法
    wsl安装Ubuntu16.04+Python2.7
    win10快速调用Shell代替GitBash
    wsl与win10文件互访
    OpenCV报错file too short解决
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4163877.html
Copyright © 2020-2023  润新知