本文整理汇总了C#中System.Runtime.Serialization.DataContractSerializer.ReadObject方法的典型用法代码示例。如果您正苦于以下问题:C# System.Runtime.Serialization.DataContractSerializer.ReadObject方法的具体用法?C# System.Runtime.Serialization.DataContractSerializer.ReadObject怎么用?C# System.Runtime.Serialization.DataContractSerializer.ReadObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.Serialization.DataContractSerializer
的用法示例。
在下文中一共展示了System.Runtime.Serialization.DataContractSerializer.ReadObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
}
示例2: 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;
}
示例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: 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);
}
示例7: 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;
}
示例8: 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;
}
示例9: 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;
}
}
}
示例10: 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;
}
}
示例11: GetObjectByType
/// <summary> Get object from isolated storage file.</summary>
/// <param name="fullpath"> file name to retreive</param>
/// <param name="type"> type of object to read</param>
/// <returns> a <c>object</c> instance, or null if the operation failed.</returns>
private async Task<object> GetObjectByType(string filepath, Type type)
{
object retVal = null;
try
{
using (var releaser = await internalManifestDiskLock.ReaderLockAsync())
{
System.Diagnostics.Debug.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now) + " internalManifestDiskLock Reader Enter for Uri: " + filepath.ToString());
byte[] bytes = await Restore(filepath);
if (bytes != null)
{
try
{
using (MemoryStream ms = new MemoryStream(bytes))
{
if (ms != null)
{
System.Runtime.Serialization.DataContractSerializer ser = new System.Runtime.Serialization.DataContractSerializer(type);
retVal = ser.ReadObject(ms);
}
}
}
catch(Exception e)
{
System.Diagnostics.Debug.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now) + " internalManifestDiskLock Reader Exception for Uri: " + filepath.ToString() + " Exception: " + e.Message);
}
}
System.Diagnostics.Debug.WriteLine(string.Format("{0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now) + " internalManifestDiskLock Reader Exit for Uri: " + filepath.ToString());
}
}
catch(Exception)
{
}
return retVal;
}
示例12: GetLanguagesForTranslate
public static List<string> GetLanguagesForTranslate()
{
string uri = "http://api.microsofttranslator.com/v2/Http.svc/GetLanguagesForTranslate?appId=" + appId;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
WebResponse response = null;
try
{
response = httpWebRequest.GetResponse();
using (Stream stream = response.GetResponseStream())
{
System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(typeof(List<string>));
return (List<string>)dcs.ReadObject(stream);
}
}
catch (WebException)
{
return new List<string>(new string[]{"en"});
}
catch
{
throw;
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
}
}
示例13: BuildMeta
public static RuntimeTypeModel BuildMeta()
{
RuntimeTypeModel model;
#if !FX11
model = TypeModel.Create();
model.Add(typeof(Order), false)
.Add(1, "OrderID")
.Add(2, "CustomerID")
.Add(3, "EmployeeID")
.Add(4, "OrderDate")
.Add(5, "RequiredDate")
.Add(6, "ShippedDate")
.Add(7, "ShipVia")
.Add(8, "Freight")
.Add(9, "ShipName")
.Add(10, "ShipAddress")
.Add(11, "ShipCity")
.Add(12, "ShipRegion")
.Add(13, "ShipPostalCode")
.Add(14, "ShipCountry");
model.Add(typeof(Product), false)
.Add(1, "ProductID")
.Add(2, "ProductName")
.Add(3, "SupplierID")
.Add(4, "CategoryID")
.Add(5, "QuantityPerUnit")
.Add(6, "UnitPrice")
.Add(7, "UnitsInStock")
.Add(8, "UnitsOnOrder")
.Add(9, "ReorderLevel")
.Add(10, "Discontinued")
.Add(11, "LastEditDate")
.Add(12, "CreationDate");
TypeModel compiled = model.Compile();
Type type = typeof(Product);
PropertyInfo[] props = type.GetProperties();
Product prod = new Product();
prod.ProductID = 123;
prod.ProductName = "abc devil";
prod.SupplierID = 456;
prod.CategoryID = 13;
prod.QuantityPerUnit = "1";
prod.UnitPrice = 12.99M;
prod.UnitsInStock = 96;
prod.UnitsOnOrder = 12;
prod.ReorderLevel = 30;
prod.Discontinued = false;
prod.LastEditDate = new DateTime(2009, 5, 7);
prod.CreationDate = new DateTime(2009, 1, 3);
DumpObject("Original", props, prod);
const int loop = 100000;
Console.WriteLine("Iterations: " + loop);
Stopwatch watch;
MemoryStream reuseDump = new MemoryStream(100 * 1024);
#if FX30
System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(type);
using (MemoryStream ms = new MemoryStream()) {
dcs.WriteObject(ms, prod);
Console.WriteLine("DataContractSerializer: {0} bytes", ms.Length);
}
watch = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
reuseDump.SetLength(0);
dcs.WriteObject(reuseDump, prod);
}
watch.Stop();
Console.WriteLine("DataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds);
watch = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
reuseDump.Position = 0;
dcs.ReadObject(reuseDump);
}
watch.Stop();
Console.WriteLine("DataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds);
{
reuseDump.Position = 0;
Product p1 = (Product) dcs.ReadObject(reuseDump);
DumpObject("DataContractSerializer", props, p1);
}
System.Runtime.Serialization.NetDataContractSerializer ndcs = new System.Runtime.Serialization.NetDataContractSerializer();
using (MemoryStream ms = new MemoryStream()) {
ndcs.Serialize(ms, prod);
Console.WriteLine("NetDataContractSerializer: {0} bytes", ms.Length);
}
watch = Stopwatch.StartNew();
for (int i = 0; i < loop; i++)
{
//.........这里部分代码省略.........
示例14: FromXml
public static RoomAvailability FromXml(string xml)
{
var deserializer = new System.Runtime.Serialization.DataContractSerializer(typeof(RoomAvailability));
using (var sr = new System.IO.StringReader(xml))
using (var reader = System.Xml.XmlReader.Create(sr))
{
return deserializer.ReadObject(reader) as RoomAvailability;
}
}
示例15: RestoreMediaFilesAsync
async private Task RestoreMediaFilesAsync()
{
string fullPath = "mediafiles.xml";
ulong size = 0;
try
{
Windows.Storage.StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fullPath);
if (file != null)
{
var prop = await file.GetBasicPropertiesAsync();
if (prop != null)
size = prop.Size;
}
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
if (stream != null)
{
using (var inputStream = stream.GetInputStreamAt(0))
{
System.Runtime.Serialization.DataContractSerializer sessionSerializer = new System.Runtime.Serialization.DataContractSerializer(typeof(Dictionary<string, MediaLibraryItem>));
defaultMediaDictionary = (Dictionary<string, MediaLibraryItem>)sessionSerializer.ReadObject(inputStream.AsStreamForRead());
fileDiscovered = (uint)defaultMediaDictionary.Count();
}
}
}
catch (Exception e)
{
LogMessage("Exception while restoring mediafiles:" + e.Message);
}
}