反序列化列表

时间:2013-05-14 08:39:54

标签: c# xml-serialization

我正在使用XmlSerializer.SerializeXmlSerializer.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文件


<?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>

2 个答案:

答案 0 :(得分:3)

很奇怪,因为以下作品对我来说非常好:

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>

答案 1 :(得分:1)

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;)。另见:How many ways can you mess up IO?