本文整理汇总了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;
}
示例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;
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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();
}
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
}
}
示例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();
}
}
}
示例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;
}
示例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;
}
}