向XML序列化添加递归文件夹结构

时间:2016-10-16 13:07:27

标签: c# xml xml-serialization

我有一个c#项目,它将对象列表序列化为xml。

[Serializable]
[XmlRoot]
public class collection {
    private List<item> _items = new List<item>();

    [XmlElement("item")]
    public List<item> items {
        get { return _items; }
    }
}

[Serializable]
    public class item {
}
然后

xml输出

<collection>
    <item/>
    <item/>
    <item/>
</collection>

我想要的是添加文件夹和子文件夹的选项,这样项目可以组合在一起,同时仍然能够进入根节点。

<collection>
    <item/>
    <item/>
    <folder>
        <item/>
        <item/>
    </folder>
    <item/>
    <item/>
    <folder>
        <item/>
        <item/>
        <folder>
            <item/>
            <item/>
        </folder>
    </folder>
    <item/>
</collection>

在继续使用序列化对象生成xml输出的同时,有人可以建议一种干净的方法吗?

2 个答案:

答案 0 :(得分:0)

这必须是班级结构。

public class Folder2
{
}

public class Folder
{
    public Folder2 folder { get; set; }
}

public class Collection
{
    public List<Folder> folder { get; set; }
}

public class RootObject
{
    public Collection collection { get; set; }
}

答案 1 :(得分:0)

经过一些试验和错误后,我设法获得了我追求的功能。

[Serializable]
[XmlRoot]
public class collection {
    private List<treeEntry> _entries = new List<treeEntry>();

    [XmlElement("item", typeof(item))]
    [XmlElement("folder", typeof(folder))]
    public List<treeEntry> entries {
        get { return _entries; }
    }
}

public abstract class treeEntry { }

[Serializable]
public class folder : treeEntry {
    private List<treeEntry> _entries = new List<treeEntry>();

    public folder() { }

    [XmlAttribute]
    public string name { get; set; }

    [XmlElement("item", typeof(item))]
    [XmlElement("folder", typeof(folder))]
    public List<treeEntry> entries {
        get { return _entries; }
    }
}


[Serializable]
public class item : treeEntry { }


public class mytest {
    public static void main() {
        collection col = new collection();
        col.entries.Add(new item());
        col.entries.Add(new item());

        var folder1 = new folder() { name = "someFolder" };
        folder1.entries.Add(new item());
        var folder2 = new folder() { name = "anotherFolder" };
        folder1.entries.Add(folder2);
        folder1.entries.Add(new item());
        folder2.entries.Add(new item());

        col.entries.Add(folder1);
        col.entries.Add(new folder());
        col.entries.Add(new item());


        XmlSerializer sers = new XmlSerializer(typeof(collection));

        //serialise
        using (StreamWriter sw = new StreamWriter("testoutput.xml", false, Encoding.UTF8)) {
            XmlWriter xw = new XmlTextWriter(sw);
            sers.Serialize(xw, col);
        }

        //deserialise
        FileStream fs = new FileStream("testoutput.xml", FileMode.Open);
        XmlReader reader = XmlReader.Create(fs);
        collection newcol = (collection)sers.Deserialize(reader);

        //output to console
        sers.Serialize(Console.Out, newcol);
    }
}

产生了所需的输出

<collection>
    <item />
    <item />
    <folder name="someFolder">
        <item />
        <folder name="anotherFolder">
            <item />
        </folder>
        <item />
    </folder>
    <folder />
    <item />
</collection>