Search code examples
c#arrayssocketstcp

how send string array using tcp in C#


I want to send a string array from server to client using TCP/IP. I tried this code but it says "Cannot assign void to an implicitly-typed variable"

string[] strarray = new [] { "0", "1", "2", "3"};
XmlSerializer serializer = new XmlSerializer(typeof(string[]));
var myString = serializer.Serialize(strarray);
// Send your string over the wire
m_writer.WriteLine(myString);
m_writer.Flush();

i know the code is incorrect but if you know any other way to send string array using tcp/ip please let me know


Solution

  • Try this:

    string[] strarray = new [] { "0", "1", "2", "3"};
    XmlSerializer serializer = new XmlSerializer(typeof(string[]));
    string myString;
    
    using (var sw = new StringWriter())
    {
        using (var xw = XmlWriter.Create(sw))
        {
            serializer.Serialize(xw, strarray);
            myString = sw.ToString();
        }
    }