• Deserializing/Serializing SOAP Messages in C#


       /// <summary>
       /// Converts a SOAP string to an object
       /// </summary>
       /// <typeparam name="T">Object type</typeparam>
       /// <param name="SOAP">SOAP string</param>
       /// <returns>The object of the specified type</returns>
       public static T SOAPToObject<T>(string SOAP)
       {
           if (string.IsNullOrEmpty(SOAP))
          {
              throw new ArgumentException("SOAP can not be null/empty");
          }

          using (MemoryStream Stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(SOAP)))
          {
              SoapFormatter Formatter = new SoapFormatter();

              return (T)Formatter.Deserialize(Stream);
          }
      }
      

      /// <summary>
      /// Converts an object to a SOAP string
      /// </summary>
      /// <param name="Object">Object to serialize</param>
      /// <returns>The serialized string</returns>
      public static string ObjectToSOAP(object Object)
      {
          if (Object == null)
          {
              throw new ArgumentException("Object can not be null");
          }

          using (MemoryStream Stream = new MemoryStream())
          {
              SoapFormatter Serializer = new SoapFormatter();
              Serializer.Serialize(Stream, Object);
              Stream.Flush();

              return UTF8Encoding.UTF8.GetString(Stream.GetBuffer(), 0, (int)Stream.Position);
          }
      }

  • 相关阅读:
    Redis系列一
    浅谈Java开发三层架构
    plsql乱码问题
    eclipse工作空间的常用设置
    《经典面试系列》- 索引
    《数据库优化》- 存储过程
    遍历Map的四种方式(Java)
    调用微信js sdk
    根据多个成对的cron表达式生成的时间段,合并
    关于Map集合注意事项
  • 原文地址:https://www.cnblogs.com/fery/p/3490813.html
Copyright © 2020-2023  润新知