M
M
Multigame2020-08-06 14:43:36
ASP.NET
Multigame, 2020-08-06 14:43:36

Why is version tag missing when serializing xml?

Good afternoon.
All of a sudden the .NET WebAPI service needed to return responses in XML format.
In principle, this is not a problem, but it confuses me that the returned XML does not contain the "XML declaration tag"
i.e. <?xml version="1.0" encoding="UTF-8" ?>
In principle, all the data of this tag is of course included in the http response headers.... But from childhood I thought that xml without this declaration is not xml

Actually the question. If we're not talking about files, but exactly http responses, is it necessary to have such a "declaration" and won't there be compatibility issues for different "clients" reading my responses?

And is it possible to somehow force C# to form this declaration without violating the standard webApi mechanism?

So far, I have found a way to form a response document in the method into the xml stream and send it as a string.
But in the regular mechanism, neither in XmlMediaTypeFormatter, nor in the returned structure itself, I found how to do it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Multigame, 2020-08-07
@Multigame

when the serialize is initialized, the declaration is formed, but for some reason this declaration disappears when it is sent from the controller.
fiddling with the msdny made it clear that there is a certain parameter OmitXmlDeclaration that should actually be false by default (that is, display a declaration)
But for some reason, the construction new XmlMediaTypeFormatter () { UseXmlSerializer = true };
Creates a serializer in controllers that has a custom value and hides the xml declaration.
In short, the fix in websettings when registering a ContentNegotiationResult for XML is to force an OmitXmlDeclaration with the desired value.

P
Peter, 2020-08-06
@petermzg

Why not go the simplest way?

var header = "<?xml version="1.0" encoding="UTF-8" ?>";
var outtext = header + serializeText;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question