示例#1
0
        /// <summary>
        /// Serialize an object into an Xml string
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SerializeObject <T>(T objToSerialize)
        {
            string       XmlString       = null;
            MemoryStream objMemoryStream = new MemoryStream();

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            StringWriter            sw = new StringWriter();;

            try
            {
                ns.Add("", "");
                XmlSerializer xs     = new XmlSerializer(typeof(T));
                XmlWriter     writer = new XmlTextWriterFormattedNoDeclaration(sw);

                xs.Serialize(writer, objToSerialize, ns);

                XmlString = sw.ToString();
                return(XmlString);
            }
            finally
            {
                objMemoryStream.Close();
                sw.Close();
            }
        }
示例#2
0
 /// <summary>
 /// Serializes the <paramref name="source"/> to XML.
 /// </summary>
 /// <param name="source">The source.</param>
 /// <returns>
 /// The <paramref name="source"/> serialized to XML.
 /// </returns>
 protected string ToXml(object source)
 {
     using (var writer = new StringWriter())
     {
         using (var xmlWriter = new XmlTextWriterFormattedNoDeclaration(writer))
         {
             this.serializer.Serialize(xmlWriter, source, this.namespaces);
             return(writer.ToString());
         }
     }
 }
示例#3
0
        public string Serialize(T responses)
        {
            if (responses == null)
            {
                return(null);
            }
            var ns = new XmlSerializerNamespaces();

            ns.Add("", "");

            var       sw     = new StringWriter();
            XmlWriter writer = new XmlTextWriterFormattedNoDeclaration(sw);

            m_Serializer.Serialize(writer, responses, ns);

            return(sw.ToString());
        }
示例#4
0
        /// <summary>
        /// Serializes as string.
        /// </summary>
        /// <param name="T">The Type of object</param>
        /// <param name="o">The object.</param>
        /// <returns>Object as XML</returns>
        public static string SerializeAsString(this object o, Type T)
        {
            var xmlSerializer  = new XmlSerializer(T);
            var writerSettings = new XmlWriterSettings();

            writerSettings.OmitXmlDeclaration = true;

            var stringWriter = new StringWriter();

            var ns = new XmlSerializerNamespaces();

            ns.Add("", "");

            using (XmlWriter xmlWriter = new XmlTextWriterFormattedNoDeclaration(stringWriter))
            {
                xmlSerializer.Serialize(xmlWriter, o, ns);
            }
            return(stringWriter.ToString());
        }
 /// <summary>
 /// Method to convert a custom Object to XML string
 /// </summary>
 /// <param name="pObject">Object that is to be serialized to XML</param>
 /// <returns>XML string</returns>
 public String SerializeObject(Object pObject)
 {
     try
     {
         //Create our own namespaces for the output
         XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
         //Add an empty namespace and empty value
         ns.Add("", "");
         String        XmlizedString = null;
         MemoryStream  memoryStream  = new MemoryStream();
         XmlSerializer xs            = new XmlSerializer(typeof(Apples));
         XmlTextWriter xmlTextWriter = new XmlTextWriterFormattedNoDeclaration(memoryStream, Encoding.UTF8);
         xs.Serialize(xmlTextWriter, pObject, ns);
         memoryStream  = (MemoryStream)xmlTextWriter.BaseStream;
         XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
         return(XmlizedString.Trim());
     }
     catch (Exception e)
     {
         System.Console.WriteLine(e);
         return(null);
     }
 }
示例#6
0
        /// <summary>
        /// Process a REST Service call
        /// </summary>
        /// <param name="restCommand">RESTCommand</param>
        /// <returns>xml string containing the response</returns>
        public string ProcessRestService(RESTCommand restCommand)
        {
            Globals.AddCurrentRemotingRequest();
            if (mDebugMode)
            {
                mServiceLog.Log.Debug("Total Remoting Service Requests: " + Globals.GetTotalRemotingRequests);
            }
            mServiceLog.Log.Debug("++++ Current Remoting Service Requests: " + Globals.GetCurrentRemotingRequests);
            string response = "";

            try
            {
                Type commandClassType = GetCommandClassType(restCommand.Noun);

                if (commandClassType == null)
                {
                    throw new ArgumentException("Service noun could not be found: " + restCommand.Noun);
                }

                MethodInfo methodInfo = commandClassType.GetMethod(restCommand.Verb);

                if (methodInfo == null)
                {
                    throw new ArgumentException("Service verb could not be found: " + restCommand.Verb);
                }
                // loop though each parameter in the method and look for argIndex corresponding
                // query parameter in the REST request URL.
                string   argString = "";
                string   delimiter = "";
                int      numArgs   = methodInfo.GetParameters().Length;
                object[] args      = new object[numArgs];

                for (int argIndex = 0; argIndex < numArgs; argIndex++)
                {
                    ParameterInfo parameterInfo = methodInfo.GetParameters()[argIndex];
                    argString += delimiter + parameterInfo.Name + "=" + Util.GetHttpValue(parameterInfo.Name);
                    delimiter  = "; ";
                    if (parameterInfo.ParameterType == typeof(int))
                    {
                        string arg = GetParameterValue(restCommand.Parameters, parameterInfo.Name);
                        int    p   = Int32.Parse(arg);
                        args[argIndex] = p;
                    }
                    else if (parameterInfo.ParameterType == typeof(string))
                    {
                        args[argIndex] = GetParameterValue(restCommand.Parameters, parameterInfo.Name);
                    }
                    else if (parameterInfo.ParameterType == typeof(PostFile))
                    {
                        PostFile postFile = new PostFile();
                        postFile       = restCommand.PostFile;
                        args[argIndex] = postFile;
                    }
                    else if (parameterInfo.ParameterType == typeof(Session.UserId))
                    {
                        string         stringUserId = GetParameterValue(restCommand.Parameters, "userId");
                        int            intUserId    = System.Convert.ToInt32(stringUserId);
                        Session.UserId userId       = new Session.UserId(intUserId);
                        args[argIndex] = userId;
                    }
                    else if (parameterInfo.ParameterType == typeof(RESTCommand))
                    {
                        args[argIndex] = restCommand;
                    }
                    else
                    {
                        throw new ArgumentException("unsupported argument type");
                    }
                }
                if (mDebugMode)
                {
                    mServiceLog.Log.Debug("ProcessRestService (remoting)(Invoke Method) (" + restCommand.Noun + ")(" + restCommand.Verb + ") args: " + argString);
                }
                Object invokeParam1 = Activator.CreateInstance(commandClassType);

                object reflectionObject = (object)methodInfo.Invoke(invokeParam1, args);

                StringWriterWithEncoding            stringWriter  = new StringWriterWithEncoding(Encoding.UTF8);
                XmlTextWriterFormattedNoDeclaration xmlTextWriter = new XmlTextWriterFormattedNoDeclaration(stringWriter);

                new XmlSerializer(methodInfo.ReturnType).Serialize(xmlTextWriter, reflectionObject);
                Globals.RemoveCurrentRemotingRequest();
                if (mDebugMode)
                {
                    mServiceLog.Log.Debug("---- Current Remoting Service Requests: " + Globals.GetCurrentRemotingRequests);
                }
                response = stringWriter.ToString();
            }

            catch (Exception ex)
            {
                Globals.RemoveCurrentRemotingRequest();
                if (mDebugMode)
                {
                    mServiceLog.Log.Debug("---- Current Remoting Service Requests: " + Globals.GetCurrentRemotingRequests);
                }
                mServiceLog.Log.Error("ProcessRequest (remoting) ERROR:" + ex.Message);
                //logError("ProcessRequest", ex);
                //response = CreateErrorDoc(ex.Message);
            }

            return(response);
        }