简体   繁体   English

使用c#读取另一台计算机中的xml文件

[英]read xml file in another computer using c#

Sorry about my bad English.抱歉我的英语不好。 I have 2 computers, name "computer A" and "computer B", both are in a LAN.我有 2 台计算机,名称为“计算机 A”和“计算机 B”,都在局域网中。 I have 2 projects.我有2个项目。 1 at computer A and the other at computer B. Computer B has a "info.xml" file in project's bin folder. 1 个在计算机 A 上,另一个在计算机 B 上。计算机 B 在项目的 bin 文件夹中有一个“info.xml”文件。 I want the project A could read that file using C#.我希望项目 A 可以使用 C# 读取该文件。 Which method should i use?我应该使用哪种方法? Thank you for taking your time.感谢您抽出宝贵时间。 This is my "Computer A" code这是我的“计算机 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();
            }

And here is "Computer B"这是“计算机 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();
            }
        }

I've tried to read my file in computer B then send it through socket.我试图在计算机 B 中读取我的文件,然后通过套接字发送它。 computer A will recieved it and write it to another xml file in computer A. but it won't work.计算机 A 将收到它并将其写入计算机 A 中的另一个 xml 文件。但它不起作用。

Here is also my "writexml" method这也是我的“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);

        }

If you only want a file to be read just read a file using the path of the file.如果您只想读取文件,只需使用文件路径读取文件。 There's no need to use sockets or so to do this.没有必要使用套接字等来做到这一点。 On the other hand, if you want to get some information from a computer to another I suggest using sockets.另一方面,如果您想从一台计算机到另一台计算机获取一些信息,我建议使用套接字。

Example of how to read a file in another computer (LAN) without sockets.如何在没有套接字的情况下读取另一台计算机 (LAN) 中的文件的示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM