反序列化 <> 类型的对象时出错 输入源格式不正确
There was an error deserializing the object of type <> The input source is not correctly formatted
我正在尝试将我的 winform c# deskltop 移植到 uwp。
在我的这部分代码中,我将网络 api 调用到 return 序列化列表。
它也被压缩了。
这是现有代码:
Uri uri = new Uri(URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsByteArrayAsync().Result;
string result2 = System.Text.Encoding.ASCII.GetString(result);
byte[] actualBytes = Convert.FromBase64String(result2.Replace("\"",""));
var myobj= Compression.DeSerialize(actualBytes);
我的压缩码:
public static byte[] Decompress(byte[] input)
{
byte[] decompressedData;
using (var outputStream = new MemoryStream())
{
using (var inputStream = new MemoryStream(input))
{
using (var zip = new GZipStream(inputStream, CompressionMode.Decompress))
{
zip.CopyTo(outputStream);
}
}
decompressedData = outputStream.ToArray();
}
return decompressedData;
}
public static Object DeSerialize(this byte[] arrBytes)
{
using (var memoryStream = new MemoryStream())
{
var binaryFormatter = new BinaryFormatter();
var decompressed = Decompress(arrBytes);
memoryStream.Write(decompressed, 0, decompressed.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
return binaryFormatter.Deserialize(memoryStream);
}
}
我的模特:
[Serializable, XmlRoot("Groups"), XmlType("Groups")]
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlElement("Group")]
public List<Group> group { get; set; }
}
, XmlRoot("Group"), XmlType("Group")]
public class 群组
{
[XmlElement("GroupRef")]
public 引导组引用;
[XmlElement("Name")]
public 字符串名称;
[XmlElement("Description")]
public 字符串说明;
}
现在将其移植到 UWP 我有这个:
Uri uri = new Uri(Shared.URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/text"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + GeneralTags.COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsByteArrayAsync().Result;
string result2 = System.Text.Encoding.ASCII.GetString(result);
byte[] actualBytes = Convert.FromBase64String(result2.Replace("\"", ""));
var myobj = Compression.DeSerialize<InformedWorker.Models.Group>(actualBytes);
我的压缩码:
public static byte[] Compress(byte[] input)
{
byte[] compressesData;
using (var outputStream = new MemoryStream())
{
using (var zip = new GZipStream(outputStream, CompressionMode.Compress))
{
zip.Write(input, 0, input.Length);
}
compressesData = outputStream.ToArray();
}
return compressesData;
}
public static T DeSerialize<T>(byte[] arrBytes)
{
var decompressed = Decompress(arrBytes);
using (MemoryStream memoryStream = new MemoryStream(decompressed))
{
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memoryStream, XmlDictionaryReaderQuotas.Max))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(T));
return (T)dcs.ReadObject(reader);
}
}
}
我的模特:
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlElement("Group")]
public List<Group> group { get; set; }
}
public class Group
{
[XmlElement("GroupRef")]
public Guid GroupRef;
[XmlElement("Name")]
public string Name;
[XmlElement("Description")]
public string Description;
}
注意:
不允许我使用“[Serializable~”,所以我不得不删除。
此外,我找不到 BinaryFormatter,所以不得不使用 'DataContractSerializer'。
这行错误:
return (T)dcs.ReadObject(reader);
错误是:
There was an error deserializing the object of type The input source is not correctly formatted.
我正在使用这种方法,以便我可以将我的数据从服务器压缩到客户端。
关于如何让它工作有什么想法吗?
谢谢
附加:
这是 XML 我是 compressing/sending 来自服务器:
<Groups>
<Group>
<GroupId>1</GroupId>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>Todays Work</Name>
<Description>System</Description>
<CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>
<Active>1</Active>
</Group>
<Group>
<GroupId>2</GroupId>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>All</Name>
<Description>System</Description>
<CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>
<Active>1</Active>
</Group>
</Groups>
等待 ReadAsByteArraysAsync。直到数据读入后,代码才会继续下一行
var result = await response.Content.ReadAsByteArrayAsync();
我正在尝试将我的 winform c# deskltop 移植到 uwp。
在我的这部分代码中,我将网络 api 调用到 return 序列化列表。
它也被压缩了。
这是现有代码:
Uri uri = new Uri(URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsByteArrayAsync().Result;
string result2 = System.Text.Encoding.ASCII.GetString(result);
byte[] actualBytes = Convert.FromBase64String(result2.Replace("\"",""));
var myobj= Compression.DeSerialize(actualBytes);
我的压缩码:
public static byte[] Decompress(byte[] input)
{
byte[] decompressedData;
using (var outputStream = new MemoryStream())
{
using (var inputStream = new MemoryStream(input))
{
using (var zip = new GZipStream(inputStream, CompressionMode.Decompress))
{
zip.CopyTo(outputStream);
}
}
decompressedData = outputStream.ToArray();
}
return decompressedData;
}
public static Object DeSerialize(this byte[] arrBytes)
{
using (var memoryStream = new MemoryStream())
{
var binaryFormatter = new BinaryFormatter();
var decompressed = Decompress(arrBytes);
memoryStream.Write(decompressed, 0, decompressed.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
return binaryFormatter.Deserialize(memoryStream);
}
}
我的模特:
[Serializable, XmlRoot("Groups"), XmlType("Groups")]
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlElement("Group")]
public List<Group> group { get; set; }
}
, XmlRoot("Group"), XmlType("Group")] public class 群组 { [XmlElement("GroupRef")] public 引导组引用; [XmlElement("Name")] public 字符串名称; [XmlElement("Description")] public 字符串说明; }
现在将其移植到 UWP 我有这个:
Uri uri = new Uri(Shared.URL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/text"));
HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + GeneralTags.COMPANY_REF + "=" + ApplicationObject.CompanyRef);
response.EnsureSuccessStatusCode();
var result = response.Content.ReadAsByteArrayAsync().Result;
string result2 = System.Text.Encoding.ASCII.GetString(result);
byte[] actualBytes = Convert.FromBase64String(result2.Replace("\"", ""));
var myobj = Compression.DeSerialize<InformedWorker.Models.Group>(actualBytes);
我的压缩码:
public static byte[] Compress(byte[] input)
{
byte[] compressesData;
using (var outputStream = new MemoryStream())
{
using (var zip = new GZipStream(outputStream, CompressionMode.Compress))
{
zip.Write(input, 0, input.Length);
}
compressesData = outputStream.ToArray();
}
return compressesData;
}
public static T DeSerialize<T>(byte[] arrBytes)
{
var decompressed = Decompress(arrBytes);
using (MemoryStream memoryStream = new MemoryStream(decompressed))
{
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(memoryStream, XmlDictionaryReaderQuotas.Max))
{
DataContractSerializer dcs = new DataContractSerializer(typeof(T));
return (T)dcs.ReadObject(reader);
}
}
}
我的模特:
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlElement("Group")]
public List<Group> group { get; set; }
}
public class Group
{
[XmlElement("GroupRef")]
public Guid GroupRef;
[XmlElement("Name")]
public string Name;
[XmlElement("Description")]
public string Description;
}
注意: 不允许我使用“[Serializable~”,所以我不得不删除。
此外,我找不到 BinaryFormatter,所以不得不使用 'DataContractSerializer'。
这行错误:
return (T)dcs.ReadObject(reader);
错误是:
There was an error deserializing the object of type The input source is not correctly formatted.
我正在使用这种方法,以便我可以将我的数据从服务器压缩到客户端。
关于如何让它工作有什么想法吗?
谢谢
附加:
这是 XML 我是 compressing/sending 来自服务器:
<Groups>
<Group>
<GroupId>1</GroupId>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>Todays Work</Name>
<Description>System</Description>
<CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>
<Active>1</Active>
</Group>
<Group>
<GroupId>2</GroupId>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>All</Name>
<Description>System</Description>
<CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>
<Active>1</Active>
</Group>
</Groups>
等待 ReadAsByteArraysAsync。直到数据读入后,代码才会继续下一行
var result = await response.Content.ReadAsByteArrayAsync();