XML保存在多个文件中



我开发了一个侦听器,它每60秒接收一次数据,它以XML data Stream的形式接收数据,这是一个完整的XML。在大多数情况下,它工作得很好,将完整的XML保存到一个4 kb的文件中。但是,有时它将一个完整的XML保存到两个文件中。我不明白为什么会发生这种事。我的代码如下。请帮助。

public static void GetXMLStream()
{
TcpListener server = null;
try
{
Int32 port = Int32.Parse(GetAppConfigValues.GetAppConfigValue("Port"));
IPAddress localAddr = IPAddress.Parse(GetAppConfigValues.GetAppConfigValue("IPAddress"));
server = new TcpListener(localAddr, port);
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
WriteToFile(data);
}
}
}
catch (SocketException e)
{}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
public static void WriteToFile(string sMessage)
{
try
{
string fileName = "NiproMI" + DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".xml";
DirectoryInfo logdirFile = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["XmlFilePath"].ToString());
string filePath = logdirFile.FullName;
if (File.Exists(Path.Combine(filePath, fileName)))
{
StreamWriter sw = null;
FileStream fs = File.Open(Path.Combine(filePath, fileName), FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
else
{
StreamWriter sw = null;
FileStream fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
}
catch (Exception e)
{
NiproEventsLog.WriteLog(e.ToString());
}
}

您应该从WriteToFile中取出文件名,因为GetXMLStream的一次调用应该只有一个文件名。

像这样:

public static void GetXMLStream()
{
string fileName = "NiproMI" + DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".xml";
TcpListener server = null;
try
{
Int32 port = Int32.Parse(GetAppConfigValues.GetAppConfigValue("Port"));
IPAddress localAddr = IPAddress.Parse(GetAppConfigValues.GetAppConfigValue("IPAddress"));
server = new TcpListener(localAddr, port);
server.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
WriteToFile(data, fileName);
}
}
}
catch (SocketException e)
{

}
finally
{
// Stop listening for new clients.
server.Stop();
}
}
public static void WriteToFile(string sMessage, string fileName)
{
try {
DirectoryInfo logdirFile = new DirectoryInfo(System.Configuration.ConfigurationManager.AppSettings["XmlFilePath"].ToString());
string filePath = logdirFile.FullName;
if (File.Exists(Path.Combine(filePath, fileName)))
{
StreamWriter sw = null;
FileStream fs = File.Open(Path.Combine(filePath, fileName), FileMode.Append, FileAccess.Write);       
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
else
{
StreamWriter sw = null;
FileStream fs = new FileStream(Path.Combine(filePath, fileName), FileMode.Create, FileAccess.Write);       
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(sMessage);
sw.Close();
sw = null;
}
}
catch (Exception e)
{
NiproEventsLog.WriteLog( e.ToString());
}
}

最新更新