在C#中将FileInfo对象序列化为XML

时间:2017-03-29 12:32:47

标签: c# xml serialization

我正在尝试将IEnumerable<FileInfo>序列化为xml。我已经研究过并发现因为FileInfo类没有无参数构造函数,所以它不能按原样序列化,我应该使用包装类。

为简单起见,我使用序列化的代码位于winforms按钮的click事件中,如图所示。一旦它工作,我就会重构:

    private void button1_Click(object sender, System.EventArgs e)
    {
        IEnumerable<FileInfo> allfiles = FileGetter.FileInfoAllFiles();

        FileList filelist = new FileList();

        foreach (var file in allfiles)
        {
            filelist.Add(new FileInfoSerializable(file));
        }

        var stream = new FileStream("Xmllist.xml", FileMode.Create);
        new XmlSerializer(typeof(FileInfoSerializable)).Serialize(stream, filelist);

    }

包装类:

[Serializable]
public class FileInfoSerializable
{

    private readonly FileInfo _fileInfo;

    #region ~~~ Constructors ~~~

    public FileInfoSerializable() { } 

    public FileInfoSerializable(FileInfo FileInfo) { _fileInfo = FileInfo; }

    #endregion


    #region ~~~ Properties ~~~

    public string Name { get { return _fileInfo.Name; } set { } }

    public string FullName { get { return _fileInfo.FullName; } set { } }

    public long Length { get { return _fileInfo.Length; } set { } }

    public string Extension { get { return _fileInfo.Extension; } set { } }

    public DateTime LastWriteTime { get { return _fileInfo.LastWriteTime; } set { } }

    public string DirectoryName { get { return _fileInfo.DirectoryName; } set { } }

    #endregion
}

我想将IEnumerable<FileInfo>中的每个FileInfo对象添加到一个集合中,然后我将序列化。这是包含集合的类:

[Serializable]
public class FileList
{
    public List<FileInfoSerializable> filez { get; set; }

    public FileList()
    {
        filez = new List<FileInfoSerializable>();
    }

    public void Add(FileInfoSerializable m)
    {
        filez.Add(m);
    }
}

问题

我在以下行获得了以下异常:new XmlSerializer(typeof(FileInfoSerializable)).Serialize(stream, filelist);

enter image description here

我已尝试使用filelist.filez代替文件列表转换为通用列表,但仍然遇到相同的错误。我需要改变什么来使这项工作?

欢呼声

2 个答案:

答案 0 :(得分:0)

我终于找到了问题。 typeof(FileInfoSerializable)应该是typeof(FileList)。以下代码有效。 FileInfoSerializable在xml的根级别创建了一个数组。 xml的根必须是单数。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication49
{
    class Program
    {

        static void Main(string[] args)
        {
            List<FileInfo> allfiles = Directory.GetFiles(@"c:\temp").Select(x => new FileInfo(x)).ToList();

            FileList filelist = new FileList();

            foreach (var file in allfiles)
            {
                filelist.Add(new FileInfoSerializable(file));
            }

            var stream = new FileStream("c:\\temp\\Xmllist.xml", FileMode.Create);

            XmlSerializer serializer = new XmlSerializer(typeof(FileList));

            serializer.Serialize(stream, filelist);


        }
    }
    [Serializable]
    public class FileInfoSerializable
    {

        private readonly FileInfo _fileInfo;

        #region ~~~ Constructors ~~~

        public FileInfoSerializable() { }

        public FileInfoSerializable(FileInfo FileInfo) { _fileInfo = FileInfo; }

        #endregion


        #region ~~~ Properties ~~~

        public string Name { get { return _fileInfo.Name; } set { } }

        public string FullName { get { return _fileInfo.FullName; } set { } }

        public long Length { get { return _fileInfo.Length; } set { } }

        public string Extension { get { return _fileInfo.Extension; } set { } }

        public DateTime LastWriteTime { get { return _fileInfo.LastWriteTime; } set { } }

        public string DirectoryName { get { return _fileInfo.DirectoryName; } set { } }

        #endregion
    }

    [Serializable]
    public class FileList
    {
        public List<FileInfoSerializable> filez { get; set; }

        public FileList()
        {
            filez = new List<FileInfoSerializable>();
        }

        public void Add(FileInfoSerializable m)
        {
            filez.Add(m);
        }
    }
}

答案 1 :(得分:0)

我已经使用您的代码进行序列化了,并且可以正常工作,但是当我反序列化列表'filez'时有项目,但是每个项目都是空的(null)。

这是反序列化的代码:

if (System.IO.File.Exists(fullName))
{
    var stream = new FileStream(fullName, FileMode.Open);                
    XmlSerializer serializer = new XmlSerializer(typeof(FileList));
    //var filelista = serializer.Deserialize(stream);
    filelist = (FileList)serializer.Deserialize(stream);
}

我附上调试信息:

I attach debug info