1

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


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

2 回答 2

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>
于 2013-05-14T08:49:11.950 回答
1

0,0 处的错误通常意味着您的 xml 文件开头的第一个元素之前有空格。仔细查看 xml(也许查看二进制文件,而不是文本 - 它也可能是 unicode 不可见空间等)。

另外:每当使用 时XmlSerializer,请查看InnerExceptions:

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?

于 2013-05-14T08:51:56.983 回答