C# 如何从TCP流读取和解析XML?

C# 如何从TCP流读取和解析XML?,c#,xml,tcp,stream,C#,Xml,Tcp,Stream,谢谢你花时间看我的问题!这是我的第一篇帖子,如果我在论坛上做错了什么,请原谅:) 编辑:我似乎已经解决了我的问题。我只是一次读取一行TCP流,然后使用if语句确定读取的开始点和停止点 更新代码: using System; using System.Net.Sockets; using System.Net; using System.IO; using System.Threading; using System.Text; namespace RFLY_CMD { class Pr

谢谢你花时间看我的问题!这是我的第一篇帖子,如果我在论坛上做错了什么,请原谅:)

编辑:我似乎已经解决了我的问题。我只是一次读取一行TCP流,然后使用if语句确定读取的开始点和停止点

更新代码:

using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Text;

namespace RFLY_CMD
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting...");
            TcpListener server = new TcpListener(IPAddress.Parse("0.0.0.0"), 7362);
            server.Start();
            Console.WriteLine("Listening...");
            Console.WriteLine();
            while (true)
            {
                ClientWorking cw = new ClientWorking(server.AcceptTcpClient());
                new Thread(new ThreadStart(cw.DoSomethingWithClient)).Start();
            }
        }
    }

    class ClientWorking
    {
        private Stream ClientStream;
        private TcpClient Client;

        public ClientWorking(TcpClient Client)
        {
            this.Client = Client;
            ClientStream = Client.GetStream();
        }

        public void DoSomethingWithClient()
        {
            StreamWriter sw = new StreamWriter(ClientStream);
            StreamReader sr = new StreamReader(sw.BaseStream);
            string data;
            bool cap = false;
            string full_string = null;
            try
            {
                while (true)
                {
                    data = sr.ReadLine();
                    if (!string.IsNullOrEmpty(data) && !string.IsNullOrWhiteSpace(data) || cap)
                    {
                        if(data == "... start")
                        {
                            cap = true;
                            Console.WriteLine("---START FOUND!!!---");
                        }

                        if (cap)
                        {
                            Console.WriteLine(data);
                            full_string += data + "--:BREAK:--";
                        }

                        if (data == "... end")
                        {
                            cap = false;

                            Console.WriteLine("---END FOUND!!!---");
                            Console.WriteLine();
                            Console.WriteLine("-------------FULL STRING-------------");
                            Console.WriteLine(full_string);
                        }
                    }
                }
            }
            finally
            {
                Console.Write("Ending...");
                sw.Close();
            }
        }
    }
}

结束编辑。

好的,问题是。。。我有一个发送XML的应用程序,如下所示:

但是,我只需要此处显示的红色框中的文本字符串:

这是我第一次处理XML,我对TCP有一些经验。除此之外,我还有点不知所措。 提前谢谢

这是我的密码:

using System;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
using System.Text;
using System.Xml;

namespace RFLY_CMD
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Starting...");
            TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 7362);
            server.Start();
            Console.WriteLine("Started.");
            while (true)
            {
                ClientWorking cw = new ClientWorking(server.AcceptTcpClient());
                new Thread(new ThreadStart(cw.DoSomethingWithClient)).Start();
            }
        }
    }

class ClientWorking
{
    private Stream ClientStream;
    private TcpClient Client;

    public ClientWorking(TcpClient Client)
    {
        this.Client = Client;
        ClientStream = Client.GetStream();
    }

    public void DoSomethingWithClient()
    {
        StreamWriter sw = new StreamWriter(ClientStream);
        StreamReader sr = new StreamReader(sw.BaseStream);
        string data;
        try
        {
            while (true)
            {
                data = sr.ReadToEnd();
                if (!string.IsNullOrEmpty(data) && !string.IsNullOrWhiteSpace(data))
                {
                    //Console.Clear();
                    Console.WriteLine(data);
                    //Console.WriteLine("---------------");
                }
                sr.DiscardBufferedData();
            }
        }
        finally
        {
            Console.Write("Ending...");
            sw.Close();
        }
    }
}

}

您当前是否从代码中获得了信息?错误或根本没有输出?代码将无法工作!!!无法判断您何时真正到达数据。TCP在不进行字节计数的情况下将消息分成数据报(最多1500字节),或者终止字符代码无法可靠地判断您何时到达数据的结尾。若要从字符串中读取Xml,请使用Parse()方法,而不是Load()方法。Parse()如果不提供完整的XML,则将给出异常。代码还将删除消息中间的空格。可能(相当遥远)您得到一个数据报,其中一个字节(一个空格)位于元素的内部文本中,甚至删除属性之间的空格导致解析错误。