14

I'm using XmlSerializer.Serialize, and it produces line breaks and unnecessary spaces. How to avoid it?

1
  • Old, but I believe the difference is whether you are using a XmlWriter or a TextWriter. In my experience, XmlWriter defaults to no formatting. This makes sense, because it knows it is writing a document where the formatting does not matter, whereas a text writer is writing straight text. Commented Jan 6, 2015 at 5:43

3 Answers 3

26

Perhaps you could use the overload accepting an XmlWriter, and configure the given XmlWriter with an XmlWriterSettings instance?

XmlWriterSettings allows you to control the application of line breaks and indentation.

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had to switch the parameters around like this: _serializer.Serialize(writer, o);. Great solution, thanks!
3

It's interesting, I thought there was no formatting by default. I just tried the following and got no formatting:

using (var stream = new MemoryStream())
{
    System.Text.Encoding encoding;
    using (var writer = XmlWriter.Create(stream))
    {
        if (writer == null)
        {
            throw new InvalidOperationException("writer is null");
        }

        encoding = writer.Settings.Encoding;
        var ser = new XmlSerializer(obj.GetType());
        ser.Serialize(writer, obj);
    }

    stream.Position = 0;
    using (var reader = new StreamReader(stream, encoding, true))
    {
        return reader.ReadToEnd();
    }
}

in a sample run, it returned the following XML:

<?xml version="1.0" encoding="utf-8"?><obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><childOne /><childTwo /><text>text1</text><text>text2</text></obj>

3 Comments

Such an inconsistency should be reported to Microsoft. A method cannot behave in different ways. It's CultureInfo dependent? Or it's a .NET Version/Service Pack? I don't know yet.
If you get a chance, run the code I posted. I'm running .NET 3.5 SP1. Let's see what the real difference is.
Your code had no whitespace in .net 4.5. But my other implementation did, and that was passing a StringWriter instance into the Serialize call's first parameter.
0

Please also check if serialized classes contain members of XML-type (like XmlElement). I used Xsd2Code tool to generate classes from XSD and (in very special case) it has created a member of type XmlElement. During serialization this member had its own formatting (identation to be exact). Even if I had turned on (or off) XmlSerializer indentation it had no affect on the XmlElement member.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.