AL icon indicating copy to clipboard operation
AL copied to clipboard

XmlDeclaration is not set in XmlDocument

Open AMCJN opened this issue 9 years ago • 8 comments

The XmlDeclaration in a XmlDocument is not set properly. The Xmldocument persit on having this XmlDeclaration header : <?xml version=“1.0” encoding=“utf-16”?>

I tryed to make the type XmlDeclaration like this :

XmlDeclaration := XmlDeclaration.Create('1','UTF-8','No'); XmlDocument.SetDeclaration(XmlDeclaration );

But when the XmlDocument is send to our webservice, the header is : <?xml version=“1.0” encoding=“utf-16”?> and not <?xml version=“1.0” encoding=“utf-8”?>

Is this a bug or am I doing something wrong ?

AMCJN avatar Sep 07 '17 09:09 AMCJN

Could you please post again what the header should be? How are you sending it?

atoader avatar Sep 07 '17 09:09 atoader

//We start by creating an XmlDocument XmlDocument := XmlDocument.Create();

//Then we try to add the XmlDeclaration to the XmlDocument XmlDeclaration := XmlDeclaration.Create('1','UTF-8','No'); XmlDocument.SetDeclaration(XmlDeclaration);

//Then we add serveral XmlElements to the XmlDocument XMLElement := XMLElement.Create(ElementName,NameSpace,ElementValue);

//Then we write the XmlDocument Content to a Text Variable TempXmlDocText XmlDocument.WriteTo(TempXmlDocText);

## //Hack to replace UTF-16 to UTF-8 //Then we make the workaround of UTF-16, replacing it with UTF-8 //This should not be necessary. EncodPos := StrPos(TempXmlDocText,'utf-16'); if(EncodPos > 0) THEN BEGIN TempXmlDocText := DelStr(TempXmlDocText,EncodPos,6); TempXmlDocText := InsStr(TempXmlDocText,'utf-8',EncodPos); END;

//Then we write the text Variable TempXmlDocText into the HttpContent //and from the HttpContent to the HttpRequestMessage HttpContent.WriteFrom(TempXmlDocText); HttpRequestMessage.Content(HttpContent);

//Then at the end we send it all with the HttpClient HttpClient.Send(HttpRequestMessage, HttpResponseMessage);

When the request hit our webservice the XmlDeclaration header is now UTF-8 but only because of the hack "replace UTF-16 to UTF-8", if we leave out this hack, the header is UTF-16, even we set the XmlDeclaration to UTF-8 at the beginning.

AMCJN avatar Sep 07 '17 10:09 AMCJN

This is a tricky one. We spent half a day discussing this when trying to decide what the best approach is. First some context: In the runtime, Text is backed by a .NET String, which represents text as a sequence of UTF-16 code units. . That is why you are getting the UTF-16 encoding, because, at that point, the XML is in UTF-16 encoding. You can get around this by serializing to a Stream instead of serializing to a Text.

atoader avatar Sep 07 '17 11:09 atoader

I tryed to change my code to this. (See below - Text in bold/Italic) It fixed the UTF-16 problem, but then I get some strange signs ´ˆŠ at the beginning of the xmldeclaration header. (´ˆŠ<?xml version="1.0" encoding="utf-8"?>)

//We start by creating an XmlDocument XmlDocument := XmlDocument.Create();

//Then we try to add the XmlDeclaration to the XmlDocument

XmlDeclaration := XmlDeclaration.Create('1','UTF-8','No'); XmlDocument.SetDeclaration(XmlDeclaration);

//Then we add serveral XmlElements to the XmlDocument XMLElement := XMLElement.Create(ElementName,NameSpace,ElementValue);

//Then we write the XmlDocument Content to a OutStream Variable XmlDocument.WriteTo(OutStream);

//Then we write the OutStream to a Text Variable TempXmlDocText OutStream.WriteText(TempXmlDocText);

//Then we read the text Variable TempXmlDocText into a InStream Variable InStream.ReadText(TempXmlDocText);

//Then we write the inStream Variable into the HttpContent //and from the HttpContent to the HttpRequestMessage HttpContent.WriteFrom(InStream); HttpRequestMessage.Content(HttpContent);

//Then at the end we send it all with the HttpClient

HttpClient.Send(HttpRequestMessage, HttpResponseMessage);

AMCJN avatar Sep 07 '17 12:09 AMCJN

You need to set the encoding on the stream to UTF-8 when you create it.

atoader avatar Sep 07 '17 12:09 atoader

Sorry, I change my code to create the streams using UTF-8, like this TempBlob.blob.CreateOutStream(sendOutStream,TextEncoding::UTF8); TempBlob.blob.CreateInStream(sendInStream,TextEncoding::UTF8);

But I still get does strange signs ´ˆŠ at the begining of the XmlDeclaration

AMCJN avatar Sep 08 '17 06:09 AMCJN

how do You add child node?

dkorinets avatar Sep 08 '17 14:09 dkorinets

@dkorinets Like this :

ParentXmlElement := XMLElement.Create(ElementName,NameSpace,ElementValue); ChildXmlElement := XMLElement.Create(ElementName,NameSpace,ElementValue); ParentXmlElement.Add(ChildXmlElement );

AMCJN avatar Sep 08 '17 16:09 AMCJN