我需要序列化复杂List<List<byte[]>>
。
我尝试了通常的XML序列化,并在里面找到了带有ArrayOfArrayOfBase64Binary
复杂元素的正确XML文件。但是反序列化时我得到了例外 -
System.InvalidOperationException: <ArrayOfArrayOfBase64Binary xmlns=''> didn't expected
这是获得的XML:
<?xml version="1.0"?>
<ArrayOfArrayOfBase64Binary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfBase64Binary>
<base64Binary>uWmpcJ5NsuN+kDdc7LHKeAQDRpt9tww2eQejjw==</base64Binary>
<base64Binary>wkHfOzURrWoibj6ayvvLvP/MC69zitU/8/rC3A==</base64Binary>
<base64Binary>io4VUpcqK0wjGx29KEUtaoB0mM4ezVVPCwwiVQ==</base64Binary>
....
<base64Binary>VB7qwOVZVzqIP2aDvEd5BqPmseMvDDt+6jJC2A==</base64Binary>
<base64Binary>bBH7CSP0e2zEDBTpOjqEPXZS5GhQ+21Aa6L9sQ==</base64Binary>
<base64Binary>g3CKGhBT9oJHn3xJCAtFL9bM+7IJCPXdJIqysg==</base64Binary>
<base64Binary>4o+aIxOu9xZ1EX/+KvaifPjyDNhBgtPZshbjUg==</base64Binary>
</ArrayOfBase64Binary>
<ArrayOfBase64Binary>
<base64Binary>4h92Duu+R8UH9seJpmDoboVVOk6YOy8GV0eCtw==</base64Binary>
<base64Binary>Oe2g/IRc5NbYZqmB/zNEx5V4kelnDpy3rjrrwg==</base64Binary>
<base64Binary>YMtYeQ3izUkQGrybfVWIzaTpVOvS0ItRN+/I4Q==</base64Binary>
....
<base64Binary>K1cYC737a7vqLvyJtq3JcR06w3ih6c2s+Ocq+g==</base64Binary>
<base64Binary>4Gc/Em29D2GZ0vm65gg65fypYficzNvG9wNvTQ==</base64Binary>
<base64Binary>SEHpodvPphyyJlLqoMM2sTWWKnJ9yDz+JkUwCQ==</base64Binary>
<base64Binary>NS5+EwbsGPOMQlkQvg8lllNi/9vaA6SxnX0nUw==</base64Binary>
</ArrayOfBase64Binary>
</ArrayOfArrayOfBase64Binary>`
序列化/反序列化的代码:
//Serialize
using (var stream = File.Create("Levin/RandomData/Z.xml"))
{
var serializer = new XmlSerializer(typeof(List<List<byte[]>>));
serializer.Serialize(stream, _Z_ext);
}
//Deserialize
using (var stream = File.OpenRead("Levin/RandomData/Z.xml"))
{
var serializer = new XmlSerializer(typeof(List<List<byte[]>>));
_Z_ext = (List<List<byte[]>>)serializer.Deserialize(stream);
}
答案 0 :(得分:1)
如果你可以使用smth而不是xml:
序列化:
var binFormatter = new BinaryFormatter();
var fileStream = File.Create(fileName);
binFormatter.Serialize(fileStream, yourListOfLists);
fileStream.Close();
并反序列化:
var binFormatter = new BinaryFormatter();
var fileStream = File.Open(fileName, FileMode.Open);
var yourListOfLists = (List<List<byte[]>>)binFormatter.Deserialize(fileStream);
fileStream.Close();
答案 1 :(得分:0)
对我而言,它运作良好。可能取决于您的数据。请试试这个:
public partial class Form1 : Form
{
List<List<byte[]>> _Z_ext = new List<List<byte[]>>();
public Form1()
{
InitializeComponent();
}
private void LoadB_Click(object sender, EventArgs e)
{
using (var stream = System.IO.File.OpenRead("Z.xml"))
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<List<byte[]>>));
_Z_ext = (List<List<byte[]>>)serializer.Deserialize(stream);
}
}
private void SaveB_Click(object sender, EventArgs e)
{
// Create some data
byte[] b1 = new byte[2] { 64, 65 };
List<byte[]> l1 = new List<byte[]>();
l1.Add(b1);
_Z_ext.Add(l1);
byte[] b2 = new byte[2] { 66, 67 };
List<byte[]> l2 = new List<byte[]>();
l2.Add(b2);
_Z_ext.Add(l2);
using (var stream = System.IO.File.Create("Z.xml"))
{
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<List<byte[]>>));
serializer.Serialize(stream, _Z_ext);
}
}
}
有用吗?如果您使用数据怎么办?