使用c#在另一台计算机中读取xml文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用c#在另一台计算机中读取xml文件相关的知识,希望对你有一定的参考价值。
抱歉我的英语不好。我有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) + "
Client 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);
}
答案
如果您只想读取文件,只需使用文件路径读取文件即可。没有必要使用套接字来执行此操作。另一方面,如果你想从计算机获取一些信息,我建议使用套接字。
如何在没有套接字的情况下读取另一台计算机(LAN)中的文件的示例。
using (var file = File.Open("//server/path/file.txt")) {...}
以上是关于使用c#在另一台计算机中读取xml文件的主要内容,如果未能解决你的问题,请参考以下文章
Apache poi java。在另一台电脑上运行JAR文件时,文档中不显示图像。