我可能已经搞砸了一个设计决定。我没有使用强类型的自定义对象集合,而是使用了通用列表。本质上,我拥有的是:public class AreaFields{ [XmlArray("Items")] [XmlArrayItem("Item")] public List<Fields> Fields { set; get; } [XmlAttribute] int id { set; get; }}public class Fields{ [XmlAttribute] public string Name { set; get; }}在整个应用程序中,我使用了List<AreaFields>现在,我需要将列表序列化为XML。我希望得到的是: <SomeXMLTag> <AreaFields id='1000'> <Items> <Item Name="Test1" /> <Item Name="Test2" /> </Items> </AreaFields> <AreaFields id='1001'> <Items> <Item Name="Test1" /> <Item Name="Test2" /> </Items> </AreaFields> </SomeXMLTag>由于我无法序列化List (或者可以吗?),因此我将不得不序列化列表的每个项目。Ex: List<AreaFields> list = new List<AreaFields>();// more code to add to list list[0].GetRawXML(); //A method i have to serialize 最佳答案 您将需要一个包装器类;然后根据您的示例序列化MyWrapper实例以获取xml。[XmlRoot("SomeXMLTag")]public class MyWrapper{ [XmlElement("AreaFields")] public List<AreaFields> AreaFields { get; set; }}public class AreaFields{ [XmlArray("Items")] [XmlArrayItem("Item")] public List<Fields> Fields { set; get; } [XmlAttribute] public int id { set; get; }}public class Fields{ [XmlAttribute] public string Name { set; get; }}关于c# - 序列化强类型对象的列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/667881/ 10-12 12:18