反序列化为列表
本文关键字:列表 反序列化 | 更新日期: 2023-09-27 18:13:40
我正在使用XmlSerializer.Serialize
和XmlSerializer.Deserialize
我有一个类MetricResult:
public class Metricresult
{
public string metric;
public string newvalue;
public string deltavalue;
}
,我正在计算一些结果,这些结果将存储在:
List<Metricresult> metricresults = new List<Metricresult>();
我成功地将这个列表更改为XML文件,在另一个项目中,我需要反序列化这个列表,但有一个异常抛出:
there-is-an-error-in-xml-document-0-0-during-deserialization
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMetricresult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Metricresult>
<metric>numberofdeclaration</metric>
<newvalue>0</newvalue>
<deltavalue>0</deltavalue>
</Metricresult>
<Metricresult>
<metric>numberofdeclaration</metric>
<newvalue>0</newvalue>
<deltavalue>0</deltavalue>
</Metricresult>
<Metricresult>
<metric>numberofdeclaration</metric>
<newvalue>0</newvalue>
<deltavalue>0</deltavalue>
</Metricresult>
<Metricresult>
<metric>numberofdeclaration</metric>
<newvalue>0</newvalue>
<deltavalue>0</deltavalue>
</Metricresult>
<Metricresult>
<metric>numberofdeclaration</metric>
<newvalue>0</newvalue>
<deltavalue>0</deltavalue>
</Metricresult>
<Metricresult>
<metric>numberofdeclaration</metric>
<newvalue>0</newvalue>
<deltavalue>0</deltavalue>
</Metricresult>
</ArrayOfMetricresult>
奇怪,因为下面的内容对我来说很好:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
public class MetricResult
{
public string Metric { get; set; }
public string NewValue { get; set; }
public string DeltaValue { get; set; }
}
class Program
{
static void Main()
{
// define some metric results to serialize
var metricResults = new[]
{
new MetricResult { Metric = "metric 1", NewValue = "new value 1", DeltaValue = "delta 1" },
new MetricResult { Metric = "metric 2", NewValue = "new value 2", DeltaValue = "delta 2" },
new MetricResult { Metric = "metric 3", NewValue = "new value 3", DeltaValue = "delta 3" },
}.ToList();
var serializer = new XmlSerializer(metricResults.GetType());
using (var writer = XmlWriter.Create("metrics.xml"))
{
// serialize the metric results we defined previously to metrics.xml
serializer.Serialize(writer, metricResults);
}
using (var reader = XmlReader.Create("metrics.xml"))
{
// read metrics.xml and deserialize back
metricResults = (List<MetricResult>)serializer.Deserialize(reader);
foreach (var result in metricResults)
{
Console.WriteLine("metric: {0}, new value: {1}, delta: {2}", result.Metric, result.NewValue, result.DeltaValue);
}
}
}
}
和metrics.xml
看起来像预期的:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMetricResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MetricResult>
<Metric>metric 1</Metric>
<NewValue>new value 1</NewValue>
<DeltaValue>delta 1</DeltaValue>
</MetricResult>
<MetricResult>
<Metric>metric 2</Metric>
<NewValue>new value 2</NewValue>
<DeltaValue>delta 2</DeltaValue>
</MetricResult>
<MetricResult>
<Metric>metric 3</Metric>
<NewValue>new value 3</NewValue>
<DeltaValue>delta 3</DeltaValue>
</MetricResult>
</ArrayOfMetricResult>
0,0的错误通常意味着在xml文件的开头,在第一个元素之前有空白。仔细查看xml(可能查看二进制,而不是文本-它也可能是unicode不可见的空格,等等)。
也:每当使用XmlSerializer
时,请查看InnerException
s:
catch(Exception ex) {
while(ex != null) {
Trace.WriteLine(ex.Message);
ex = ex.InnerException;
}
}
通常有非常详细的解释,如果你从头到尾看一遍消息。
人们常犯的一个错误是在使用流时,不倒带。下面的语句将返回0,0错误: using (var ms = new MemoryStream())
{
serializer.Serialize(ms, metricResults);
serializer.Deserialize(ms);
}
,因为序列化在最后离开了流。如果您有这样的代码,请将流重置到序列化和反序列化之间的起始位置(ms.Position = 0;
)。参见:你有多少种方式可以搞砸IO?