Xml的对象未序列化byte []数据 [英] Object to Xml not serializing byte[] data
问题描述
大家好.
我正在序列化对象/将其转换为XML数据
并传递到另一个页面,然后再次反序列化.
我正在使用以下功能进行序列化和反序列化
hi to all.
I am serializing my object / converting it to XML data
and passing to another page and again Deserializing it.
i am using following function to serialize and Deserializing
public static string Serialize(object objectToSerialize)
{
MemoryStream mem = new MemoryStream();
XmlSerializer ser = new XmlSerializer(objectToSerialize.GetType());
ser.Serialize(mem, objectToSerialize);
ASCIIEncoding ascii = new ASCIIEncoding();
return ascii.GetString(mem.ToArray());
}
public static object Deserialize(string xmlString)
{
Type typeToDeserialize;
byte[] bytes = Encoding.UTF8.GetBytes(xmlString);
typeToDeserialize =xmlString.GetType();
MemoryStream mem = new MemoryStream(bytes);
XmlSerializer ser = new XmlSerializer(typeof (User ));
return ser.Deserialize(mem);
}
我将对象User设置为
i have object User as
public class User
{
public string UserId { get; set; }
public string Depatment { get; set; }
public string Role { get; set; }
internal byte[] FPTemplates { get; set; }
}
现在当我对其进行序列化时,我只会得到前三个元素
FPTemplates是byte [],它在xml文件中不存在
我应该怎么做才能获得对象的完全序列化.
请帮我...
在此先感谢... :)
上帝保佑你们..:)
now when i am serializing it i m getting only first three elements
FPTemplates which is byte[] it is not present in xml file
what should i do to get full serialization of object.??
plz help me...
thank in advance...:)
god bless u all.. :)
推荐答案
我看到了您的问题.
您的字节数组是内部的,使其公开
I see your issue.
Your byte array is internal make it public
<pre lang="c#">
public byte[] FPTemplates { get; set; }
</pre>
尝试执行此操作.我看到您正在将字符串转换为utf8,然后在序列化程序中将ascii转换为
Try giving this a Go I see you are converting your string to utf8 and then ascii in your serialisers
/// <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 obj)
{
try
{
string xmlString = null;
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(typeof(T));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xs.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
xmlString = Converters.ByteArrayToUTF8String(memoryStream.ToArray()); return xmlString;
}
catch
{
return string.Empty;
}
}
/// <summary>
/// Reconstruct an object from an XML string
/// </summary>
/// <param name="xml"></param>
/// <returns></returns>
public static T DeserializeObject<t>(string xml)
{
XmlSerializer xs = new XmlSerializer(typeof(T));
MemoryStream memoryStream = new MemoryStream(Converters.StringToUTF8ByteArray(xml));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return (T)xs.Deserialize(memoryStream);
}
public class Converters{
/// <summary>
/// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
/// </summary>
/// <param name="characters">Unicode Byte Array to be converted to String</param>
/// <returns>String converted from Unicode Byte Array</returns>
public static string ByteArrayToUTF8String(byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
string constructedString = encoding.GetString(characters);
return (constructedString);
}
/// <summary>
/// Converts the String to UTF8 Byte array and is used in De serialization
/// </summary>
/// <param name="stringVal"></param>
/// <returns></returns>
public static Byte[] StringToUTF8ByteArray(string stringVal)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(stringVal);
return byteArray;
}
}
</t></t>
这篇关于Xml的对象未序列化byte []数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!