使用C#在另一台计算机上读取xml文件

时间:2019-03-05 01:57:12

标签: c# .net sockets

对不起,我的英语不好。 我有2台计算机,分别是局域网中的“计算机A”和“计算机B”。 我有2个项目。 1在计算机A上,另一在计算机B上。 计算机B在项目的bin文件夹中有一个“ info.xml”文件。 我希望项目A可以使用C#读取该文件。 我应该使用哪种方法? 感谢您抽出宝贵的时间。 这是我的“计算机A”代码

namespace Client
{
    class Program
    {
        private const int BUFFER_SIZE = 1024;
        private const int PORT_NUMBER = 7826;
        public static string xmlsvinfo = Directory.GetCurrentDirectory() + "\\data\\serverinfo.xml";
        public static string xmlpath = Directory.GetCurrentDirectory() + "\\data\\gamesinfo.xml";
        static ASCIIEncoding encoding = new ASCIIEncoding();
        static void Main(string[] args)
        {
            try
            {
                // IPAddress address = IPAddress.Parse("127.0.0.1");
                IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), PORT_NUMBER);

                Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                client.Connect(iep);
                string command = "checkupdate";
                while (!command.Equals("quit"))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(xmlsvinfo);
                    XmlNode node = doc.SelectSingleNode(@"root/UPDATE");
                    string clupdate = node.InnerText;
                    // gui lenh
                    if (command == "update")
                    {
                        XmlDocument docx = new XmlDocument();
                        docx.Load(xmlpath);
                        docx.DocumentElement.RemoveAll();
                        docx.Save(xmlpath);
                        string[] mtam = new string[4];
                        client.Send(encoding.GetBytes(command));
                        for (int i = 0; i < 4; i++)
                            {
                                byte[] data = new byte[BUFFER_SIZE];
                                int rec = client.Receive(data);
                                mtam[i] = encoding.GetString(data, 0, rec);
                                Console.WriteLine("da nhan: " + mtam[i]);
                            }
                        write_xml(mtam[0], mtam[1], mtam[2], mtam[3]);
                        Console.ReadLine();
                        client.Close();
                    }
                    else
                    {
                        client.Send(encoding.GetBytes(command));
                        byte[] data = new byte[BUFFER_SIZE];
                        int rec = client.Receive(data);
                        Console.WriteLine("Server version: " + encoding.GetString(data, 0, rec) + "\nClient version: " + clupdate);
                        if (clupdate == encoding.GetString(data, 0, rec))
                        {
                            command = "quit";
                        }
                        else
                        {
                            command = "update";
                            clupdate = encoding.GetString(data, 0, rec);
                            node.InnerText = clupdate;
                            doc.Save(xmlsvinfo);
                        }
                        // Console.ReadLine();
                    }
                }

                client.Close();
                // Console.ReadLine();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex);
                // Console.ReadLine();
            }

这是“计算机B”

namespace Server
{
    class Program
    {
        private const int BUFFER_SIZE = 1024;
        private const int PORT_NUMBER = 7826;
        public static string xmlsvinfo = Directory.GetCurrentDirectory() + "\\data\\serverinfo.xml";
        public static string xmlpath = Directory.GetCurrentDirectory() + "\\data\\gamesinfo.xml";

        static ASCIIEncoding encoding = new ASCIIEncoding();
        static void Main(string[] args)
        {
            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), PORT_NUMBER);

                Console.WriteLine("waiting for client...");

                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                server.Bind(iep);
                server.Listen(10);

                Socket client = server.Accept();
                Console.WriteLine("Accepted: " + client.RemoteEndPoint.ToString());

                byte[] data = new byte[BUFFER_SIZE];
                string result = "";
                while (true)
                {
                    int rec = client.Receive(data);
                    string command = encoding.GetString(data, 0, rec);
                    Console.WriteLine("Client: " + command);
                    if (command.Equals("checkupdate"))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(xmlsvinfo);
                        XmlNode node = doc.SelectSingleNode(@"root/UPDATE");
                        result = node.InnerText;
                    }
                    else if (command.Equals("update"))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(xmlpath);
                        XmlNodeList nodelist = doc.GetElementsByTagName("gameinfor");
                        string dem = nodelist.Count.ToString();
                        client.Send(encoding.GetBytes(dem));
                        string dat = string.Empty;
                        for(int i = 0; i<nodelist.Count; i++)
                        {
                            for(int j= 0; j<4; j++)
                            {
                                dat = nodelist[i].ChildNodes.Item(j).InnerText;
                                client.Send(encoding.GetBytes(dat));
                                Console.WriteLine("sending " + dat);
                            }
                        }
                        Console.ReadLine();
                        client.Close();
                        break;
                    }
                    else if (command.Equals("quit"))
                    {
                        client.Close();
                        break;
                    }
                    else
                    {
                        result = "wrong command";
                    }
                    client.Send(encoding.GetBytes(result));
                    Console.ReadLine();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex);
                Console.ReadLine();
            }
        }

我试图在计算机B中读取我的文件,然后通过套接字发送它。计算机A将收到它,并将其写入计算机A中的另一个xml文件中。但是它将无法正常工作。

这也是我的“ writexml”方法

static void write_xml(string id, string name, string cata, string path)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlpath);

            XmlNode gameinfor = xmlDoc.CreateNode(XmlNodeType.Element, "gameinfor", null);

            XmlNode nodeId = xmlDoc.CreateElement("ID_Game");
            nodeId.InnerText = id;

            XmlNode nodegamename = xmlDoc.CreateElement("Tên_Game");
            nodegamename.InnerText = name;

            XmlNode nodetheloai = xmlDoc.CreateElement("Thể_Loại");
            nodetheloai.InnerText = cata;

            XmlNode nodegamepath = xmlDoc.CreateElement("Path");
            nodegamepath.InnerText = path;

            gameinfor.AppendChild(nodeId);
            gameinfor.AppendChild(nodegamename);
            gameinfor.AppendChild(nodetheloai);
            gameinfor.AppendChild(nodegamepath);

            xmlDoc.DocumentElement.AppendChild(gameinfor);
            xmlDoc.Save(xmlpath);

        }

1 个答案:

答案 0 :(得分:0)

如果只希望读取文件,则使用文件的路径读取文件。无需使用套接字即可。另一方面,如果您想从计算机上获取一些信息,我建议使用套接字。

如何在没有套接字的情况下在另一台计算机(LAN)中读取文件的示例。

using (var file = File.Open("//server/path/file.txt")) {...}