我正在使用XmlSerializer.Serialize,它会产生换行符和不必要的空格。如何避免呢?

最佳答案

也许您可以使用接受XmlWriter的重载,并使用 XmlWriter 实例配置给定的XmlWriterSettings
XmlWriterSettings允许您控制换行和缩进的应用。

void Serialize(Object o)
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = false;
    settings.NewLineHandling = NewLineHandling.None;
    //settings.OtherProperties = values;

    using (XmlWriter writer = XmlWriter.Create(CreateStream(), settings))
    {
        _serializer.Serialize(writer, o);
    }
}

09-19 06:16