When I build a XML in C# using XmlDocument, and I want to save to XML to file, including the encoding information, I use XmlTextWriter, as below:
using (StringWriter swr = new StringWriter())
{
using (XmlTextWriter xtw = new XmlTextWriter(swr))
{
xmlDoc.WriteTo(xtw);
return swr.ToString();
}
}
With the above code, the String I get back has the following syntax:
<?xml version="1.0" encoding="utf-8"?>
<regs>
<reg1>
....
</reg1>
</regs>
I would like to have the same behavior using the IXMLDOMDocument methods. In this cenario, the only way I know to extract the XML string is through the xmlDoc.xml
method. However, using this method, the string is quite different, as is the encoding:
<?xml version="1.0"?>
<regs>
<reg1>
....
</reg1>
</regs>
Is there a way to output a IXMLDOMDocument the same way I get with the XmlTextWriter, and with the same encoding results?
Tks
Edit
The code that I use to generate the XML through DOM is in Delphi:
function TXMLClass.GenerateXML: Variant;
var
iCont: Integer;
sName, sValor: String;
vXML: Variant;
oNodeDados, oNodeCliente, oNodeTransacao: Variant;
oHeader: Variant;
begin
vXML := CreateOLEObject('Msxml2.DOMDocument.6.0');
try
oHeader := vXML.createProcessingInstruction('xml', 'version=''1.0'' encoding=''utf-8''');
vXML.AppendChild(oHeader);
oNodeDados := vXML.CreateElement('regs');
vXML.AppendChild(oNodeDados);
oNodeCliente := vXML.CreateElement('reg1');
oNodeDados.AppendChild(oNodeCliente);
Result := vXML;
except
on e: Exception do
begin
vXML := Unassigned;
Result := vXML;
raise;
end;
end;
end;
My main problem is the resulting encoding of the string, because I transmit the resulting WideString to a C# WebService, and when I read it in a XmlDocument, the characters with any accent are all wrong. When I generate the XML in C#, export it through the XmlTextWriter, and send it back to Delphi, and I load it through DOM, the characters are correct.
Edit
When I use the vXML.Save(file_name.xml)
, the saved file is coded correctly, and if I load it into a WideString (Unicode string in Delphi), and transmit it to the Web Service, it works out nice. But how can I do it without saving it to disk, and through DOM?