当前位置: 首页>>代码示例>>C#>>正文


C# Serialization.DataContractSerializer类代码示例

本文整理汇总了C#中System.Runtime.Serialization.DataContractSerializer的典型用法代码示例。如果您正苦于以下问题:C# System.Runtime.Serialization.DataContractSerializer类的具体用法?C# System.Runtime.Serialization.DataContractSerializer怎么用?C# System.Runtime.Serialization.DataContractSerializer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


System.Runtime.Serialization.DataContractSerializer类属于命名空间,在下文中一共展示了System.Runtime.Serialization.DataContractSerializer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TranslateMethod

        private string TranslateMethod(string authToken, string text)
        {
            string translation = string.Empty;
            string from = "en";
            string to = "ja";

            string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text="
                + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Headers.Add("Authorization", authToken);
            WebResponse response = null;
            try
            {
                response = httpWebRequest.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    System.Runtime.Serialization.DataContractSerializer dcs =
                        new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                    translation = (string)dcs.ReadObject(stream);
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }

            return translation;
        }
开发者ID:moyoron64,项目名称:eyeX,代码行数:33,代码来源:TranslatorApi.cs

示例2: TranslateMethod

        public static string TranslateMethod(string authToken, string originalS, string from, string to)
        {
            string text = originalS; //"你能听见我";
            //string from = "en";
            //string to = "zh-CHS";
            //string from = Constants.from;// "zh-CHS";
            //string to = Constants.to; // "en";

            string transuri = ConstantParam.ApiUri + System.Net.WebUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(transuri);
            //  httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Headers["Authorization"] = authToken;
            //httpWebRequest.Method = "GET";
            string trans;

            Task<WebResponse> response = httpWebRequest.GetResponseAsync();

            using (Stream stream = response.Result.GetResponseStream())
            {
                System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                //DataContractJsonSerializer dcs = new DataContractJsonSerializer(Type.GetType("System.String"));
                trans = (string)dcs.ReadObject(stream);
                return trans;

            }
        }
开发者ID:WinkeyWang,项目名称:SRTranslator,代码行数:27,代码来源:Translator.cs

示例3: Translate

        public override string Translate(string text)
        {
            string result = "nothing yet...";

            Stream stream = null;

            try
            {
                using (stream = GetResponse(text).GetResponseStream())
                {
                    var dataContractSerializer
                        = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                    result = (string) dataContractSerializer.ReadObject(stream);
                }
            }
            catch (WebException e)
            {
                result = e.Message;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }

            return result;
        }
开发者ID:mauriliofilho,项目名称:SelectAndTranslate,代码行数:29,代码来源:BingTranslator.cs

示例4: Deserialize

        public object Deserialize(string serializedObject, Type type)
        {
            using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(serializedObject)))
            {
                var serializer = new System.Runtime.Serialization.DataContractSerializer(type, null, Int32.MaxValue, false, false, null, _dataContractResolver);

                return serializer.ReadObject(memoryStream);
            }
        }
开发者ID:AtmosphereMessaging,项目名称:Cumulus,代码行数:9,代码来源:DataContractSerializer.cs

示例5: using

 /// <summary>
 /// Creates a new object that is a copy of the current instance.
 /// </summary>
 /// <returns>
 /// A new object that is a copy of this instance.
 /// </returns>
 object ICloneable.Clone()
 {
     var serializer = new System.Runtime.Serialization.DataContractSerializer(GetType());
     using (var ms = new System.IO.MemoryStream())
     {
         serializer.WriteObject(ms, this);
         ms.Position = 0;
         return serializer.ReadObject(ms);
     }
 }
开发者ID:Tony-Liang,项目名称:RBAC,代码行数:16,代码来源:EntityBase.cs

示例6: GetXml

 internal static string GetXml(object o) {
   var formatter = new System.Runtime.Serialization.DataContractSerializer(o.GetType());
   using (var stringWriter = new StringWriter())
   using (var xmlWriter = new XmlTextWriter(stringWriter)) {
     xmlWriter.Formatting = Formatting.Indented;
     xmlWriter.QuoteChar = '\'';
     formatter.WriteObject(xmlWriter, o);
     return stringWriter.ToString();
   }
 }
开发者ID:Prover,项目名称:Prover.SpecWriter,代码行数:10,代码来源:DataContractFormatter.cs

示例7: TestSer

        public void TestSer()
        {
            var f = new SomeTestingViewModel() {  Result="hahahaha"};

            var dx = new System.Runtime.Serialization.DataContractSerializer(typeof(SomeTestingViewModel));
            var s = new MemoryStream();
            dx.WriteObject(s, f);
            s.Position = 0;
            var s2 = dx.ReadObject(s) as SomeTestingViewModel;
            Assert.AreEqual(s2.Result, f.Result);
        }
开发者ID:taojunfeng,项目名称:MVVM-Sidekick,代码行数:11,代码来源:SerTest.cs

示例8: InternalLoadSnapshotAsync

 internal async Task<IEnumerable> InternalLoadSnapshotAsync()
 {
     var f = await workingFile;
     var sl = new System.Runtime.Serialization.DataContractSerializer(typeof(PicLibFolder[]));
     var ms = new MemoryStream();
     using (var inps = (await f.OpenReadAsync()).GetInputStreamAt(0).AsStreamForRead())
     {
         await inps.CopyToAsync(ms);
         ms.Position = 0;
     }
     var r = sl.ReadObject(ms) as PicLibFolder[];
     return r;
 }
开发者ID:waynebaby,项目名称:GreaterShareUWP,代码行数:13,代码来源:PicLibFolderScanService.cs

示例9: ToBinary

 /// <summary>
 /// Returns a byte array that represents the current <see cref="T:System.Object"/>. 
 /// </summary>
 /// <returns>A byte array that represents the current <see cref="T:System.Object"/>.</returns>
 public virtual byte[] ToBinary()
 {
     byte[] buffer;
     using (var ms = new System.IO.MemoryStream())
     using (var writer = System.Xml.XmlDictionaryWriter.CreateBinaryWriter(ms))
     {
         var serializer = new System.Runtime.Serialization.DataContractSerializer(GetType());
         serializer.WriteObject(writer, this);
         writer.Flush();
         buffer = ms.ToArray();
     }
     return buffer;
 }
开发者ID:Tony-Liang,项目名称:RBAC,代码行数:17,代码来源:EntityBase.cs

示例10: DataContractDeserialize

        public static object DataContractDeserialize(string buffer, bool compress)
        {
            if (!string.IsNullOrEmpty(buffer))
            {
                int idx = buffer.IndexOf('@');
                string assemblyQualifiedName = buffer.Substring(0, idx);
                string objBuffer = buffer.Substring(idx + 1);
                Type objType = Type.GetType(assemblyQualifiedName, true);

                System.Runtime.Serialization.DataContractSerializer aa = new System.Runtime.Serialization.DataContractSerializer(objType);
                XmlReader reader = XmlReader.Create(new StringReader(objBuffer));
                return aa.ReadObject(reader);
            }
            else
                return null;
        }
开发者ID:shelgaerel,项目名称:MobileApplication,代码行数:16,代码来源:Utility.cs

示例11: TranslateString

        /// <summary>
        /// Used to perform the actual translation
        /// </summary>
        /// <param name="InputString"></param>
        /// <returns></returns>
        public override string TranslateString(string InputString)
        {
            Console.WriteLine("Processing: " + InputString);
            string result = "";

            using (WebClient client = new WebClient())
            {
                using (Stream data = client.OpenRead(this.BuildRequestString(InputString)))
                {
                    System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
                    result = (string)dcs.ReadObject(data);

                    data.Close();
                }
            }

            return result;
        }
开发者ID:chrislbennett,项目名称:AndroidTranslator,代码行数:23,代码来源:MicrosoftAPI.cs

示例12: GetLanguageNames

        public static string[] GetLanguageNames(string[] languageCodes)
        {
            string uri = "http://api.microsofttranslator.com/v2/Http.svc/GetLanguageNames?locale=" + languageCodes[0] + "&appId=" + appId;
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.ContentType = "text/xml";
            httpWebRequest.Method = "POST";
            System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String[]"));
            using (System.IO.Stream stream = httpWebRequest.GetRequestStream())
            {
                dcs.WriteObject(stream, languageCodes);
            }

            WebResponse response = null;
            try
            {
                response = httpWebRequest.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    return (string[])dcs.ReadObject(stream);
                }
            }
            catch (WebException)
            {
                if (languageCodes.Length == 1 && languageCodes[0] == "en")
                    return new string[] { "English" };
                else
                    throw;
            }
            catch
            {
                throw;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
            }
        }
开发者ID:cchitsiang,项目名称:Multi-Lingual-Chat,代码行数:41,代码来源:BingServicesClient.cs

示例13: Serialize

        public string Serialize(object serializableObject)
        {
            using (var memoryStream = new MemoryStream())
            {
                var serializer = new System.Runtime.Serialization.DataContractSerializer(serializableObject.GetType(),
                                                                                         null,
                                                                                         Int32.MaxValue,
                                                                                         false,
                                                                                         false,
                                                                                         null,
                                                                                         _dataContractResolver);

                serializer.WriteObject(memoryStream, serializableObject);
                memoryStream.Seek(0, SeekOrigin.Begin);

                using (var streamReader = new StreamReader(memoryStream))
                {
                    return streamReader.ReadToEnd();
                }
            }
        }
开发者ID:AtmosphereMessaging,项目名称:Cumulus,代码行数:21,代码来源:DataContractSerializer.cs

示例14: DataContractSerialize

        public static string DataContractSerialize(Object obj, bool compress)
        {
            if (obj != null)
            {
                Type objType = obj.GetType();
                System.Runtime.Serialization.DataContractSerializer aa = new System.Runtime.Serialization.DataContractSerializer(objType);
                StringBuilder sb = new StringBuilder();

                // Inserisce l'assembly qualified name nello stream del buffer come primo elemento separato dalla @
                sb.Append(objType.AssemblyQualifiedName);
                sb.Append('@');

                XmlWriter writer = XmlWriter.Create(sb);
                aa.WriteObject(writer, obj);
                writer.Close();

                return sb.ToString();
            }
            else
                return null;
        }
开发者ID:shelgaerel,项目名称:MobileApplication,代码行数:21,代码来源:Utility.cs

示例15: TranslateMethod

        public static string TranslateMethod(string authToken, string originalS, string from, string to)
        {
            string text = originalS; 
            string transuri = ConstantParam.ApiUri + System.Net.WebUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;

            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(transuri);

            httpWebRequest.Headers["Authorization"] = authToken;
            string trans;

            Task<WebResponse> response = httpWebRequest.GetResponseAsync();

            using (Stream stream = response.Result.GetResponseStream())
            {
                System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));

                trans = (string)dcs.ReadObject(stream);
                return trans;

            }
        }
开发者ID:abhaybagai,项目名称:DataCultureSeries,代码行数:21,代码来源:Translator.cs


注:本文中的System.Runtime.Serialization.DataContractSerializer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。