XML反序列化错误IEnumerable类

时间:2015-06-22 02:37:48

标签: c# xml xml-serialization xml-deserialization

我正在尝试将HistoryRoot类串行和删除为此XML格式:

<?xml version="1.0"?>
<HistoryRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Files>
    <HistoryItem d="2015-06-21T17:40:42" s="file:///D:\cars.txt" />
  </Files>
  <Folders>
    <HistoryItem d="2015-06-21T17:40:42" s="D:\fc\Cars" />
  </Folders>
</HistoryRoot>

这是HistoryRoot,HistoryList和HistoryItem类:

[Serializable]
public class HistoryRoot
{
    public HistoryList
    Files = new HistoryList
    {
        sl = new SortedList<DateTime, string>(),
        list = new List<HistoryItem>(),
        max = 500,
        c = program.M.qFile
    },
    Folders = new HistoryList
    {
        sl = new SortedList<DateTime, string>(),
        list = new List<HistoryItem>(),
        max = 100,
        c = program.M.qFolder
    },
}

[Serializable]
public class HistoryList : IEnumerable
{
    [XmlIgnore]
    public List<HistoryItem> list;

    [XmlIgnore]
    public SortedList<DateTime, string> sl;

    [XmlIgnore]
    public int max;

    [XmlIgnore]
    public ComboBox c;

    public IEnumerator GetEnumerator()
    {
        if (list == null) list = new List<HistoryItem>();
        return list.GetEnumerator();
    }
}

public struct HistoryItem
{
    [XmlAttribute("d")]
    public DateTime D;

    [XmlAttribute("s")]
    public string S;
}

这是我收到错误的地方:

using (FileStream fs = new FileStream("filepath.xml", FileMode.Open))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(HistoryRoot));
        HistoryRoot h = (HistoryRoot)serializer.Deserialize(fs);
    }

“反映类型'History.HistoryRoot'时出错。” System.Exception {System.InvalidOperationException}
我该如何解决这个错误?谢谢!

2 个答案:

答案 0 :(得分:2)

为了使用XmlSerializer序列化或反序列化实现IEnumerable的类,您的类必须具有Add方法。来自documentation

  

XmlSerializer为实现IEnumerable或ICollection的类提供特殊处理。实现IEnumerable的类必须实现一个带有单个参数的公共Add方法。 Add方法的参数必须与从GetEnumerator返回的值的Current属性返回的类型相同,或者该类型的base之一。

即使您从未反序列化也只能序列化,您必须拥有此方法,因为XmlSerializer同时为序列化和反序列化生成运行时代码。

该方法实际上不必工作以便序列化成功,它只需要存在

    public void Add(object obj)
    {
        throw new NotImplementedException();
    }

(当然,要成功 de 序列化,必须实施该方法。)

答案 1 :(得分:1)

虽然sbc的回答是正确的并且我接受了,但我现在将HistoryList类更改为更容易:

public class HistoryList : List<HistoryItem> //   <-- Add List<HistoryItem>
{    
    [XmlIgnore]
    public SortedList<DateTime, string> sl;

    [XmlIgnore]
    public int max;

    [XmlIgnore]
    public ComboBox c;
}

然后将HistoryRoot更改为:

[Serializable]
public class HistoryRoot
{
    public HistoryList
    Files = new HistoryList
    {
        sl = new SortedList<DateTime, string>(),
        //list = new List<HistoryItem>(),   <-- Remove this line
        max = 500,
        c = program.M.qFile
    },
    Folders = new HistoryList
    {
        sl = new SortedList<DateTime, string>(),
        //list = new List<HistoryItem>(),   <-- Remove this line
        max = 100,
        c = program.M.qFolder
    },
}
相关问题