为什么XML在序列化后有空格而不是数据?

时间:2014-08-14 13:02:07

标签: c# xml serialization xml-serialization

序列化后,有时xml文件会填充空格。为什么会这样? 我的序列化代码是:

XmlSerializer serializer = new XmlSerializer(typeof(SerializableStorage));
    using (StreamWriter sw = new StreamWriter(storageName, false, myEncoding))
    {
       serializer.Serialize(sw, messages);
    }

此代码将消息序列化为XML。 SerializableStorage看起来像:

[XmlRoot("Storage")]
public class SerializableStorage
{
    /// <summary>
    /// Gets or sets an array of server messages
    /// </summary>
    [XmlArray("Messages")]
    [XmlArrayItem("Message")]
    public List<NMessage> Messages
    {
        get;
        protected set;
    }

    /// <summary>
    /// Public constructor
    /// </summary>
    public SerializableStorage()
    {
        Messages = new List<NMessage>();
    }
}

NMessage类将所有字段都公开。

如果需要,反序列化:

XmlSerializer serializer = new XmlSerializer(typeof(SerializableStorage));
StreamReader reader = new StreamReader(storageName, myEncoding);
StoreMessages = (SerializableStorage)serializer.Deserialize(reader);
reader.Close();

我附加了一个“空xml”示例here

0 个答案:

没有答案