开发者

C# - From NetworkStream to XElement managing different encoding

开发者 https://www.devze.com 2023-02-19 03:48 出处:网络
I\'ve an application that search XML over the network (using TcpClient), these XML have various encoding (one site in UTF8, other in Windows-1252). I would like encode all of these XML in UTF-8 (alway

I've an application that search XML over the network (using TcpClient), these XML have various encoding (one site in UTF8, other in Windows-1252). I would like encode all of these XML in UTF-8 (always) to be sure I'm clean.

How can I do the conversion from the NetworkStream to an XElement encoding correctly all data?

I've this :

NetworkStream _clientStream = /* ... */;
MemoryStream _responseBytes = new MemoryStream();

// serverEncoding -> Xml Encoding I get from server
// _UTF8Encoder -> Local encoder (always UTF8)

try
{
    _clientStream.CopyTo(_responseBytes);

    if (serverEncoding != _UTF8Encoder)
    {
        MemoryStream encodedStream = new MemoryStream();
        string line = null;
        using (StreamReader reader = new StreamReader(_responseBytes))
        {
            using (StreamWriter writer = new StreamWriter(encodedStream))
       开发者_运维百科     {
                while ((line = reader.ReadLine()) != null)
                {
                    writer.WriteLine(
                        Encoding.Convert(serverEncoding, _UTF8Encoder, serverEncoding.GetBytes(line))
                        );
                }
            }
        }
        _responseBytes = encodedStream;
    }

    _responseBytes.Position = 0;
    using (XmlReader reader = XmlReader.Create(_responseBytes))
    {
        xmlResult = XElement.Load(reader, LoadOptions.PreserveWhitespace);
    }
}
catch (Exception ex)
{ }

Have you a better solution (and by ignoring all '\0' ?).


Edit

This works :

byte[] b = _clientStream.ReadToEnd();
var text = _UTF8Encoder.GetString(b, 0, b.Length);
xmlResult = XElement.Parse(text, LoadOptions.PreserveWhitespace);

But this not :

using (var reader = new StreamReader(_clientStream, false))
    xmlResult = XElement.Load(reader, LoadOptions.PreserveWhitespace);

I don't understand why ...


You can simply create a StreamReader around the NetworkStream, passing the encoding of the stream, then pass it to XElement.Load:

XElement elem
using(var reader = new StreamReader = new StreamReader(_clientStream, serverEncoding))
    elem = XElement.Load(reader);

There is no point in manually transcoding it to a different encoding.

0

精彩评论

暂无评论...
验证码 换一张
取 消