csteinmueller
csteinmueller

Reputation: 2487

How to send UTF-8 coded XML without receiving umlauts in entity notation

I'm sending data to a business partner. The demand of the partner is clear: UTF-8 signed XML. Furthermore they want to receive german umlauts not being in entity notation.

This german site tells it decimal notation. But it means the same.

So how to tell the Encoding how to send the umlauts?

Thanks in advance!

//edit: the business partner confirmed they receive the umlauts in entity notation (&#123)

//edit2: here's the code I use to send the data

WebClient client = new WebClient
{
    Proxy = new WebProxy("wwwproxy", 80)
    {
        Credentials = CredentialCache.DefaultCredentials
    }
};
byte[] response = client.UploadData(
    Data.Resources.gateway_uri,
    Encoding.UTF8.GetBytes(request.ToString(SaveOptions.DisableFormatting)));

return XDocument.Parse(System.Text.Encoding.UTF8.GetString(response));

Upvotes: 1

Views: 908

Answers (1)

Codo
Codo

Reputation: 78805

I'm guessing that request is an XNode instance and I'm guessing that its ToString method tries to avoid encoding problems by encoding most characters outside the ASCII range as it cannot know what the final encoding will be.

To have better control over the encoding, you'll need to use an XML writer with a stream:

MemoryStream buffer = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8; // should be the default, but you never know
XMLWriter writer = XMLWriter.Create(buffer, settings);
request.WriteTo(writer);
writer.Flush();
byte[] requestData = buffer.ToArray();
byte[] response = client.UploadData(Data.Resources.gateway_uri, requestData);

Upvotes: 1

Related Questions