编辑:由于它们是我之前回答的几个缺陷 - 没有包含更多条的 bar 并且没有使用 xmlAttributes 这里是我的新解决方案:但是这没有实现......
编辑:嗯,我已经回去回顾了一些事情,这是我通过实现 ICollection 的最终解决方案,希望这可以帮助任何试图找到序列化集合的解决方案的人。
public class Program
{
static void Main(string[] args)
{
var program = new Program();
program.SerializeObject();
}
private int tierIndex = 1;
public void SerializeObject()
{
var barCollection = new BarCollection();
var bar1 = new Bar() { Name = "bar1" };
barCollection.Add(bar1);
var bar2 = new Bar() { Name = "bar2" };
barCollection.Add(bar2);
var bar3 = new Bar() { Name = "bar3" };
bar2.BarCollection.Add(bar3);
var bar4 = new Bar() { Name = "bar4" };
bar2.BarCollection.Add(bar4);
var bar5 = new Bar() { Name = "bar 5" };
barCollection.Add(bar5);
var bar6 = new Bar() { Name = "bar 6" };
barCollection.Add(bar6);
var bar7 = new Bar() { Name = "bar 7" };
bar6.BarCollection.Add(bar7);
var bar8 = new Bar() { Name = "bar 8" };
bar7.BarCollection.Add(bar8);
var x = new XmlSerializer(typeof(BarCollection));
x.Serialize(Console.Out, barCollection);
Console.WriteLine("\n");
WriteCollection(barCollection);
Console.ReadLine();
}
public void WriteCollection(BarCollection barCollection)
{
tierIndex++;
foreach (Bar bar in barCollection)
{
Console.Write(new StringBuilder().Insert(0, "--", tierIndex) + "> ");
Console.Write(bar.Name + "\n");
WriteCollection(bar.BarCollection);
}
tierIndex--;
}
}
public class BarCollection : ICollection
{
private readonly ArrayList barNodes = new ArrayList();
public Bar this[int index]
{
get { return (Bar) barNodes[index]; }
}
public void CopyTo(Array a, int index)
{
barNodes.CopyTo(a, index);
}
public int Count
{
get { return barNodes.Count; }
}
public object SyncRoot
{
get { return this; }
}
public bool IsSynchronized
{
get { return false; }
}
public IEnumerator GetEnumerator()
{
return barNodes.GetEnumerator();
}
public void Add(Bar bar)
{
barNodes.Add(bar);
}
public void Add(Object bar)
{
barNodes.Add((Bar) bar);
}
}
public class Bar
{
[XmlAttribute(AttributeName = "Name")]
public string Name;
[XmlArray(ElementName = "BarNodes", IsNullable = true)]
public BarCollection BarCollection = new BarCollection();
}
这是输出:
<?xml version="1.0" encoding="IBM437"?>
<ArrayOfBar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Bar Name="bar1">
<BarNodes />
</Bar>
<Bar Name="bar2">
<BarNodes>
<Bar Name="bar3">
<BarNodes />
</Bar>
<Bar Name="bar4">
<BarNodes />
</Bar>
</BarNodes>
</Bar>
<Bar Name="bar 5">
<BarNodes />
</Bar>
<Bar Name="bar 6">
<BarNodes>
<Bar Name="bar 7">
<BarNodes>
<Bar Name="bar 8">
<BarNodes />
</Bar>
</BarNodes>
</Bar>
</BarNodes>
</Bar>
</ArrayOfBar>
----> bar1
----> bar2
------> bar3
------> bar4
----> bar 5
----> bar 6
------> bar 7
--------> bar 8
另一个堆栈参考:XmlSerializer 不会序列化 IEnumerable