gpt4 book ai didi

c# - 反序列化到列表

转载 作者:太空宇宙 更新时间:2023-11-03 21:46:50 26 4
gpt4 key购买 nike

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

最佳答案

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

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>

关于c# - 反序列化到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16538594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com