这是HelloBox类的结构 [Serializable] public class HelloBox { public byte[] pic; public string text; public HelloBox(); }
是这样转换么?如果是的话还是不行 HelloBox [Serializable] public class HelloBox { public string pic; public string text; public HelloBox(); }Client Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999); socket.Connect(ep); HelloBox helloBox = new HelloBox(); helloBox.text = text; MemoryStream ms1 = new MemoryStream(); pic.Save(ms1, System.Drawing.Imaging.ImageFormat.Bmp); byte[] bytePic = ms1.ToArray(); helloBox.pic = System.Convert.ToBase64String(bytePic); XmlSerializer xs = new XmlSerializer(typeof(HelloBox)); MemoryStream ms2 = new MemoryStream(); xs.Serialize(ms2, helloBox); byte[] data = new byte[1024]; ms2.Position = 0; while (ms2.Read(data, 0, 1024) > 0) { socket.Send(data); } socket.Shutdown(SocketShutdown.Both); socket.Close();Server socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999); socket.Bind(ep); socket.Listen(100); Socket client = socket.Accept(); XmlSerializer xs = new XmlSerializer(typeof(HelloBox)); MemoryStream ms = new MemoryStream();
byte[] data = new byte[1024]; while (client.Receive(data) > 0) { ms.Write(data,0,1024); } ms.Position = 0; HelloBox helloBox = xs.Deserialize(ms) as HelloBox;//这行就出错了 return helloBox;哪位能帮忙看看? 另外小弟在这祝大家虎年吉祥~
错误信息是 There is an error in XML document (5, 12)
学习,帮你顶一下,另外你为什么非要用XmlSerializer?
可以将Image保存在Byte[]中!然后再XML序列化
xml序列化不需要那个Attribute声明就可以。怎么看出不行呢?不懂你是怎么看的。
奥,是个异常,异常信息在14楼, 具体一点的话exception: System.InvalidOperationExceptionstacktrace: at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle) at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)innerException: {"Data at the root level is invalid. Line 5, position 12."}data: {System.Collections.ListDictionaryInternal}
在自定义的类中,有一个List<Image>字段,但对这个类的一个实例序列化后,在反实例化时报错,我想知道问什么,如何解决
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999); socket.Connect(ep); HelloBox helloBox = new HelloBox();
helloBox.text = text;
helloBox.pic = pic; XmlSerializer xs = new XmlSerializer(typeof(HelloBox));
MemoryStream ms = new MemoryStream(); xs.Serialize(ms, helloBox); byte[] data = new byte[1024]; ms.Position = 0;
while (ms.Read(data, 0, 1024) > 0)
{
socket.Send(data);
}
socket.Shutdown(SocketShutdown.Both);
socket.Close();
Server端 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999); socket.Bind(ep);
socket.Listen(100); Socket client = socket.Accept(); XmlSerializer xs = new XmlSerializer(typeof(HelloBox));
MemoryStream ms = new MemoryStream();
byte[] data = new byte[1024]; while (client.Receive(data) > 0)
{
ms.Write(data,0,1024);
} ms.Position = 0;
HelloBox helloBox = xs.Deserialize(ms) as HelloBox; return helloBox;
问题在哪里?还请各位指正
Image image = VerifyCode.NextImage(6);
//XmlSerializer s = new XmlSerializer(image.GetType());
BinaryFormatter s = new BinaryFormatter();
using(FileStream fs = new FileStream("1.bin", FileMode.Create))
{
s.Serialize(fs, image);
}
using(FileStream fs = new FileStream("1.bin", FileMode.Open))
{
image = s.Deserialize(fs) as Image;
}
image.Save("1.png", ImageFormat.Png);
http://msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlserializer(VS.80).aspx基元类型(如:整数、字符串和字节数组)以及某些特殊类型(如 XmlElement 和 DateTime),这些特殊类型也被视为基元类型。
数据协定类型(用 DataContractAttribute 属性标记的类型)。
用 SerializableAttribute 属性标记的类型,包括实现 ISerializable 接口的类型。
实现 IXmlSerializable 接口的类型。
许多常见集合类型,包括许多泛型集合类型。
许多 .NET Framework 类型属于后两种类别,因此可序列化。可序列化类型的数组也可序列化。有关完整列表,请参见在服务协定中指定数据传输。
public class HelloBox
{
public byte[] pic;
public string text; public HelloBox();
}
HelloBox [Serializable]
public class HelloBox
{
public string pic;
public string text; public HelloBox();
}Client Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999); socket.Connect(ep); HelloBox helloBox = new HelloBox();
helloBox.text = text; MemoryStream ms1 = new MemoryStream();
pic.Save(ms1, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bytePic = ms1.ToArray();
helloBox.pic = System.Convert.ToBase64String(bytePic); XmlSerializer xs = new XmlSerializer(typeof(HelloBox));
MemoryStream ms2 = new MemoryStream();
xs.Serialize(ms2, helloBox); byte[] data = new byte[1024]; ms2.Position = 0;
while (ms2.Read(data, 0, 1024) > 0)
{
socket.Send(data);
}
socket.Shutdown(SocketShutdown.Both);
socket.Close();Server socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ep = new IPEndPoint(Dns.GetHostEntry("127.0.0.1").AddressList[0], 9999); socket.Bind(ep);
socket.Listen(100); Socket client = socket.Accept(); XmlSerializer xs = new XmlSerializer(typeof(HelloBox));
MemoryStream ms = new MemoryStream();
byte[] data = new byte[1024]; while (client.Receive(data) > 0)
{
ms.Write(data,0,1024);
} ms.Position = 0;
HelloBox helloBox = xs.Deserialize(ms) as HelloBox;//这行就出错了
return helloBox;哪位能帮忙看看?
另外小弟在这祝大家虎年吉祥~
There is an error in XML document (5, 12)
xml序列化不需要那个Attribute声明就可以。怎么看出不行呢?不懂你是怎么看的。
奥,是个异常,异常信息在14楼,
具体一点的话exception:
System.InvalidOperationExceptionstacktrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)innerException:
{"Data at the root level is invalid. Line 5, position 12."}data:
{System.Collections.ListDictionaryInternal}
while ((br=client.Receive(data)) > 0)
{
ms.Write(data,0,br);
}