c# - 如何序列化数组<>到XML? (获取“你必须在System.Array上实现一个默认访问器,因为它继承自ICollection。”)

时间:2009-10-09 00:59:05

标签: c# arrays xml-serialization

我有点难过。我真的很想要XML序列化一个数组<>但我得到:“你必须在System.Array上实现一个默认访问器,因为它继承自ICollection。”

我的代码中的代码段如下。有什么想法吗?

    Array a = Files.ToArray();
    XmlSerializer serializer = new XmlSerializer(typeof(Array));
    TextWriter textWriter = new StreamWriter(CONFIG_FILE_PATH);
    serializer.Serialize(textWriter, a);
    textWriter.Close();

感谢

PS。这是Files对象的样子:

private SerializableDictionary<string, ConfigFileDTO> files = new SerializableDictionary<string, ConfigFileDTO>();

public class ConfigFileDTO
        {
            private string path;
            private string content_type;
            private long file_size;
            private DateTime updated_at;
            private HttpStatusCode status_code;
            private bool discoveryAttempted = false;

            private List<ConfigFileDTO> parents = new List<ConfigFileDTO>();
            private List<ConfigFileDTO> children = new List<ConfigFileDTO>();
            .
            .
            .

SerializableDictionary详细信息(由其他人提供给我)是:

public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();
        if (wasEmpty)
            return;
        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");
            reader.ReadStartElement("key");
            TKey key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();
            reader.ReadStartElement("value");
            TValue value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();
            this.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
    #endregion

}

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

Object[] a = Files.ToArray();
XmlSerializer serializer = new XmlSerializer(typeof(Object[]));
TextWriter textWriter = new StreamWriter(@"d:\test.txt");
serializer.Serialize(textWriter, a);
textWriter.Close();

如果你知道从方法返回的数组的类型就这样(我使用Object[],因为我不知道它返回的内容)。