无论出于何种原因,在序列化期间 XmlSerializer 都不包含空列表。我没有找到太多关于这种行为是否正确或是否可以被覆盖的文档。我包含了我尝试序列化的类型的代码和序列化代码,希望有人能对此有所了解。
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/FacilitySettings.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org/FacilitySettings.xsd", IsNullable=true)]
public class WeightSettings
{
public List<string> WeightOZIdentifiers
{
get
{
return this.weightOZIdentifiersField;
}
set
{
this.weightOZIdentifiersField = value;
}
}
}
public static string ToXmlString<T>(this T obj)
{
var builder = new StringBuilder();
using (var stringWriter = new StringWriter(builder))
{
var xml = new XmlTextWriter(stringWriter);
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(xml, obj);
return builder.ToString();
}
}
编辑以反映