zoukankan      html  css  js  c++  java
  • WebService 序列化和反序列化

    参考了Fish LI的Xml读取文章,写了XML序列化和反序列化的文章。

    序列化:把实体列转化成XML。反序列化:把XML按一定的规则转化成需要的实体列。

    序列化和反序列化化使用到的类,

    using System.Xml.Serialization;
    using System.IO;
    using System.Xml;

    XML一些属性:

    XMLAttribtue:属性

    XMLText:xml值

    XmlElement:xml标签值

    XML调试,反序列化失败。常见的原因是XML的定义和Class类不一致,导致失败。我的做法是把class序列化,比较序列化的值和需要反序列化的XML有什么不同。

    代码使用的类,Class C

     public class C
        {
            public string Company
            {
                get;
                set;
            }
    
            [XmlElement("B2")]
            public List<B> b
            {
                get;
                set;
            }
        }
    
    
        public class B
        {
            [XmlAttribute("sex")]
            public string B_Attribute
            {
                get;
                set;
            }
    
            [XmlAttribute("age")]
            public string B_Attribute_Age
            {
                get;
                set;
            }
    
            [XmlText]
         
            public string B_Name
            {
                get;
                set;
            }
        }
    View Code

    1. 序列化代码使用到的方法类,MemoryStream 初始化流,XmlWriter 创建Stream流可读写,serializer.Serialize序列化操作。StreamReader 读取序列化的流信息。

    代码,

       public static string XmlSerialize(object o, Encoding encoding)
            {
                XmlSerializer serializer = new XmlSerializer(o.GetType());
    
                // StreamWriter 方法
                //using (MemoryStream stream = new MemoryStream())
                //{
                //    using (StreamWriter writer = new StreamWriter(stream, encoding))
                //    {
                //        serializer.Serialize(writer, o);
                //        stream.Position = 0;
                //        using (StreamReader reader = new StreamReader(stream, encoding))
                //        {
                //            return reader.ReadToEnd();
                //        }
                //    }
                //}
    
                // XmlWriting方法
                using (MemoryStream stream = new MemoryStream())
                {
                    using (XmlWriter writer = XmlWriter.Create(stream))
                    {
                        serializer.Serialize(writer, o);
                    }
    
                    stream.Position = 0;
    
                    using (StreamReader reader = new StreamReader(stream, encoding))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
    View Code

    2.反序列化化使用到的方法类,MemoryStream 流的形式初始化XML到内存中,StreamReader读取XML二进制信息,mySerializer.Deserialize反序列化为Class C

            public static T XmlDeserialize<T>(string s, Encoding encoding)
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
                {
                    using (StreamReader sr = new StreamReader(ms, encoding))
                    {
                        return (T)mySerializer.Deserialize(sr);
                    }
                }
            }
    View Code

     所有的代码如下,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.IO;
    using System.Xml;
    
    namespace WebService
    {
        class Program
        {
            static void Main(string[] args)
            {
                //C c = new C();
                //List<B> listB = new List<B>();
    
                //B b = new B();
                //b.A = "男";
                //listB.Add(b);
    
                //b = new B();
                //b.A = "女";
                //listB.Add(b);
    
                //b = new B();
                //b.A = "男";
                //listB.Add(b);
    
                //c.b = listB;
                //string str = XmlSerialize(c, Encoding.UTF8);
                //Console.WriteLine(str);
    
                //C cClass = XmlDeserialize<C>(@"<C><B2><A sex=""男"" /><AA>陈建</AA></B2></C>", Encoding.UTF8);
    
                // 最主要的是类和xml信息能对应,反序列化失败的查找方法,先序列化出来一个XML,对比XML值。
                C cClass = XmlDeserialize<C>(@"<C><Company>ctrip</Company><B2 sex=""男"" age=""31"" >chinaagan</B2></C>", Encoding.UTF8);
                B b1 = cClass.b[0];
                Console.WriteLine(cClass.Company);
                Console.WriteLine("sex:" + b1.B_Attribute);
                Console.WriteLine("age:" + b1.B_Attribute_Age);
                Console.WriteLine(b1.B_Name);
                Console.Read();
            }
    
            public static string XmlSerialize(object o, Encoding encoding)
            {
                XmlSerializer serializer = new XmlSerializer(o.GetType());
    
                // StreamWriter 方法
                //using (MemoryStream stream = new MemoryStream())
                //{
                //    using (StreamWriter writer = new StreamWriter(stream, encoding))
                //    {
                //        serializer.Serialize(writer, o);
                //        stream.Position = 0;
                //        using (StreamReader reader = new StreamReader(stream, encoding))
                //        {
                //            return reader.ReadToEnd();
                //        }
                //    }
                //}
    
                // XmlWriting方法
                using (MemoryStream stream = new MemoryStream())
                {
                    using (XmlWriter writer = XmlWriter.Create(stream))
                    {
                        serializer.Serialize(writer, o);
                    }
    
                    stream.Position = 0;
    
                    using (StreamReader reader = new StreamReader(stream, encoding))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
    
    
            public static T XmlDeserialize<T>(string s, Encoding encoding)
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s)))
                {
                    using (StreamReader sr = new StreamReader(ms, encoding))
                    {
                        return (T)mySerializer.Deserialize(sr);
                    }
                }
            }
        }
    
        public class C
        {
            public string Company
            {
                get;
                set;
            }
    
            [XmlElement("B2")]
            public List<B> b
            {
                get;
                set;
            }
        }
    
    
        public class B
        {
            [XmlAttribute("sex")]
            public string B_Attribute
            {
                get;
                set;
            }
    
            [XmlAttribute("age")]
            public string B_Attribute_Age
            {
                get;
                set;
            }
    
            [XmlText]
         
            public string B_Name
            {
                get;
                set;
            }
        }
    }
    View Code

    跟上节拍,每日累积一点技术。

  • 相关阅读:
    lincode 题目记录5
    lintcode题目记录4
    lintcode 题目记录3
    lintcode 题目记录2
    剖析ASP.NET Core(Part 2)- AddMvc(译)
    剖析ASP.NET Core MVC(Part 1)- AddMvcCore(译)
    如何 RESTFul 你的服务(译)
    Windows + IIS 环境部署Asp.Net Core App
    Asp.Net Core 缓存的使用(译)
    [转载].NET Core 轻量级模板引擎 Mustachio
  • 原文地址:https://www.cnblogs.com/chinaagan/p/WebService.html
Copyright © 2011-2022 走看看