[英]Read an XML Data from TCP Port Socket
I have a device which send me data over a particular port. 我有一台通过特定端口向我发送数据的设备。 The data is in XML Format.
数据为XML格式。
Now I already did a small console application which listen for the data and print out the data on the screen. 现在,我已经做了一个小型控制台应用程序,可以监听数据并在屏幕上打印出数据。
Now my plans are to deserialize the xml data or I will create the insert statement into the database. 现在我的计划是反序列化xml数据,否则我将在数据库中创建插入语句。
1) The device send me the data every second (if there is data to send me). 1)设备每秒发送一次数据给我(如果有数据发送给我)。 2) I cannot say how much data the device is going to send me.
2)我无法说出设备将向我发送多少数据。
How can I make sure to capture all data without loose any information.. 如何确保在不丢失任何信息的情况下捕获所有数据。
Ready to discuss? 准备讨论了吗?
CODE in C# or VB.NET C#或VB.NET中的代码
Per XML document, you will need to read continously from the socket stream until you find a matching root end element; 对于每个XML文档,您将需要从套接字流中连续读取,直到找到匹配的根端元素为止;
<?xml version='1.0' encoding='utf-8'?><rootElement> ... </rootElement>
Done, and wait for the next document. 完成,然后等待下一个文档。
As Hans Passant points out, TCP is designed for accurate delivery. 正如Hans Passant指出的,TCP旨在实现准确的交付。 To receive all tcp data sent during a connection (which sounds like your case), use the code in my answer at C# performance methods of receiving data from a socket?
要接收连接期间发送的所有tcp数据(听起来像您的情况),请在我的答案中使用C#性能方法中的代码从套接字接收数据? .
。
Once you've received all of the bytes, you can do the typical conversion to text, eg string stringData = Encoding.UTF8.GetString(bytes);
收到所有字节后,即可进行典型的文本转换,例如
string stringData = Encoding.UTF8.GetString(bytes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.