网络传输——XML文档的序列化

服务器端:

using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;

namespace 输出
{
    class Program
    {
        static void Main(string[] args)
        {

            Connect con = new Connect();
            Thread thread = new Thread(new ThreadStart(con.WaitForConnect));
            thread.Start();
          
        }
    }

    public class Connect
    {
        TcpListener Listener;
        TcpClient Client;
        NetworkStream Stream;

        public void WaitForConnect()
        {
            Listener = new TcpListener(IPAddress.Any, 8888);
            Listener.Start();


            Client = Listener.AcceptTcpClient();
            Stream = Client.GetStream();

            XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));
            XmlDocument doc = (XmlDocument)ser.Deserialize(Stream);
            doc.Save("C://Server.xml");
        }
}
}

 

客户端:

using System.Net.Sockets;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;

namespace 客户端
{
    static class Program
    {
        static void Main()
        {
            Connect con = new Connect();
            Thread thread = new Thread(new ThreadStart(con.WaitForConnect));
            thread.Start();
        }
    }

    public class Connect
    {
        public TcpListener Listener;
        public TcpClient Client;
        public NetworkStream Stream;
        public void WaitForConnect()
        {
            Client = new TcpClient("127.0.0.1", 8888);
            Stream = Client.GetStream();

            XmlDocument doc = new XmlDocument();
            doc.Load("C://Client.xml");
            XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));
            ser.Serialize(Stream, doc);
            Stream.Close();
        }
   
    }
}

这是服务器端和客户端之间一个XML文档的传输,XML需要在传输之前建立好

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值