espero este código te ayude.
using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp1
{
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class NODO1
{
private string pARAM1Field;
private string pARAM2Field;
private string pARAM3Field;
private string pARAM4Field;
private string pARAM5Field;
private string pARAM6Field;
private string pARAM7Field;
private string pARAM8Field;
/// <remarks/>
public string PARAM1
{
get
{
return this.pARAM1Field;
}
set
{
this.pARAM1Field = value;
}
}
/// <remarks/>
public string PARAM2
{
get
{
return this.pARAM2Field;
}
set
{
this.pARAM2Field = value;
}
}
/// <remarks/>
public string PARAM3
{
get
{
return this.pARAM3Field;
}
set
{
this.pARAM3Field = value;
}
}
/// <remarks/>
public string PARAM4
{
get
{
return this.pARAM4Field;
}
set
{
this.pARAM4Field = value;
}
}
/// <remarks/>
public string PARAM5
{
get
{
return this.pARAM5Field;
}
set
{
this.pARAM5Field = value;
}
}
/// <remarks/>
public string PARAM6
{
get
{
return this.pARAM6Field;
}
set
{
this.pARAM6Field = value;
}
}
/// <remarks/>
public string PARAM7
{
get
{
return this.pARAM7Field;
}
set
{
this.pARAM7Field = value;
}
}
/// <remarks/>
public string PARAM8
{
get
{
return this.pARAM8Field;
}
set
{
this.pARAM8Field = value;
}
}
}
public static class MyExtension
{
public static string SerializeObject<T>(this T toSerialize, Encoding encoding)
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
Encoding = encoding
};
XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
using (var stream = new MemoryStream())
{
using (var xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
xmlSerializer.Serialize(xmlWriter, toSerialize);
}
return encoding.GetString(stream.ToArray());
}
}
}
class Program
{
private static readonly string hostname = "host"; // recuperar de configuración
private static readonly int port = 1234; // recuperar de configuración
static void Main(string[] args)
{
var nodo = new NODO1()
{
PARAM1 = "PARAM1",
PARAM2 = "PARAM2",
PARAM3 = "PARAM3",
PARAM4 = "PARAM4",
PARAM5 = "PARAM5",
PARAM6 = "PARAM6",
PARAM7 = "PARAM7",
PARAM8 = "PARAM8",
};
SendMessage(nodo);
Console.ReadKey();
}
private static void SendMessage(NODO1 nodo)
{
var encoding = Encoding.GetEncoding("ISO-8859-1");
var message = nodo.SerializeObject<NODO1>(encoding);
SendTCP(message, encoding);
}
private static void SendTCP(string message, Encoding encoding)
{
string response;
using (var client = new TcpClient(hostname, port))
{
using (var stream = client.GetStream())
{
using (var writer = new StreamWriter(stream, encoding))
{
writer.AutoFlush = false;
writer.Write(encoding.GetBytes(message).Length);
writer.Write(message);
writer.Flush();
StreamReader reader = new StreamReader(stream, encoding);
response = reader.ReadLine();
writer.Close();
stream.Close();
client.Close();
}
}
}
}
}
}