你可以去這個,如果你想用適當的產能來初始化列表:
Int32 capacity = courselvl4.Count + courselvl5.Count + courselvl6.Count;
List<Course> courses = new List<Course>(capacity);
courses.AddRange(courselvl4);
courses.AddRange(courselvl5);
courses.AddRange(courselvl6);
或者:
List<Course> courses = courselvl4
.Concat(courselvl5)
.Concat(courselvl6)
.ToList();
然後只是去:
SerializeCourses("CoursesList",courses);
private static XDocument SerializeCourses(String rootElement, List<Course> courses)
{
XDocument doc = new XDocument();
using (XmlWriter writer = doc.CreateWriter())
{
writer.WriteStartElement(rootElement);
foreach (Course course in courses)
new XmlSerializer(typeof(Course)).Serialize(writer, course);
writer.WriteEndElement();
}
return doc;
}
您可能嘗試序列化'courselvl4.Union(courselvl5).Union(courselvl6)'在你的情況下聯盟的結果將是IEnumerable類型 –