Serializing class structure to XML seems to add a NewLine character

428 Views Asked by At

The code below serializes XML into a string, then writes it to an XML file (yes quite a bit going on with respect to UTF8 and removal of the Namespace):

var bidsXml = string.Empty;

var emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;

activity = $"Serialize Class INFO to XML to string";
using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
  XmlSerializer xml = new XmlSerializer(info.GetType());

  xml.Serialize(writer, info, emptyNamespaces);

  bidsXml = Encoding.UTF8.GetString(stream.ToArray());
}

var lastChar = bidsXml.Substring(bidsXml.Length);

var fileName = $"CostOffer_Testing_{DateTime.Now:yyyy.MM.dd_HH.mm.ss}.xml";

var path = $"c:\\temp\\pjm\\{fileName}";
File.WriteAllText(path, bidsXml);

Problem is, serialization to XML seems to introduce a CR/LF (NewLine):

enter image description here

It's easier to see in the XML file:

enter image description here

A workaround is to strip out the "last" character:

bidsXml = bidsXml.Substring(0,bidsXml.Length - 1);

But better is to understand the root cause and resolve without a workaround - any idea why this a NewLine characters is being appended to the XML string?

** EDIT **

I was able to attempt a load into the consumer application (prior to this attempt I used an API to import the XML), and I received a more telling message:

The file you are loading is a binary file, the contents can not be displayed here.

So i suspect an unprintable characters is somehow getting embedded into the file/XML. When I open the file in Notepad++, I see the following (UFF-8-Byte Order Mark) - at least I have something to go on:

enter image description here

1

There are 1 best solutions below

3
Bill Roberts On

So it seems the consumer of my XML does not want BOM (Byte Order Mark) within the stream.

Visiting this site UTF-8 BOM adventures in C#

I've updated my code to use new UTF8Encoding(false)) rather than Encoding.UTF8:

var utf8NoBOM = new UTF8Encoding(false);

var bidsXml = string.Empty;

var emptyNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });

var settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;

activity = $"Serialize Class INFO to XML to string";
using (MemoryStream stream = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream, utf8NoBOM))
{
  XmlSerializer xml = new XmlSerializer(info.GetType());

  xml.Serialize(writer, info, emptyNamespaces);

  bidsXml = utf8NoBOM.GetString(stream.ToArray());
}


var fileName = $"CostOffer_Testing_{DateTime.Now:yyyy.MM.dd_HH.mm.ss}.xml";

var path = $"c:\\temp\\pjm\\{fileName}";
File.WriteAllText(path, bidsXml, utf8NoBOM);