I am sending data to a Socket and connection and retrieving some data. The data I am retrieving (which is an XML document) contains umlauts like ä,ü,ö,… but these are not correctly encoded, I only get ? or )))@ symbols instead of the umlaut.
Is there something incorrect how I retrieve the data ? The data is send back as ISO-8859-1 encoding.
My code:
public bool GetData()
{
try
{
TcpClient tcpClient = new TcpClient("THEIP", 5004);
NetworkStream ns = tcpClient.GetStream();
if (ns.CanWrite)
{
string text = this.xmlRequest;
byte[] sendBytes = Encoding.ASCII.GetBytes(text);
string sendData = Encoding.ASCII.GetString(sendBytes);
ns.Write(sendBytes, 0, sendBytes.Length);
}
System.Threading.Thread.Sleep(2000);
int i = 0;
if (ns.CanRead)
{
do
{
byte[] recvBytes = new byte[tcpClient.ReceiveBufferSize];
i = i + ns.Read(recvBytes, 0, tcpClient.ReceiveBufferSize);
string returnData = Encoding.ASCII.GetString(recvBytes);
returnData = returnData.Substring(0, i);
this.xmlResponse = returnData;
} while (ns.DataAvailable);
}
// got full data...
bool status = this.ProcessXMLData();
ns.Close();
if (status)
{
return true;
}
else
{
return false;
}
}
catch (SocketException se)
{
this.errorFlag = true;
this.errorMessage = se.Message;
return false;
}
}
If you are sure about the encode is being used is ISO-8859-1 then Instead of
Encoding.ASCIIuseEncoding.GetEncoding("ISO-8859-1")