我写的第一个程序中写XML的代码执行速度有些问题,改了一下,现在有所改善。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Data.SqlClient; using System.Xml; using System.IO; using System.Collections; using System.Threading; namespace StockUpService { public partial class StockUpService : ServiceBase { public StockUpService() { InitializeComponent(); if (!System.Diagnostics.EventLog.SourceExists("MySource")) { System.Diagnostics.EventLog.CreateEventSource( "MySource", "MyNewLog"); } eventLog1.Source = "MySource"; eventLog1.Log = "MyNewLog"; } protected override void OnStart(string[] args) { System.Timers.Timer t = new System.Timers.Timer(20000); //实例化Timer类,设置间隔时间为20000毫秒|20秒执行一次; t.Elapsed += new System.Timers.ElapsedEventHandler(runservice); //到达时间的时候执行事件; t.AutoReset = true; //设置是执行一次(false)还是一直执行(true); t.Enabled = true; //是否执行System.Timers.Timer.Elapsed事件; } public void runservice(object source, System.Timers.ElapsedEventArgs e) { string t; t = System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Minute.ToString(); if (t == "933") { readXml(); } } public static void readXml() { string classId=""; string filialeId; string startTime; string endTime; string companyType; string xmlChildNodes = ""; string dsn; string outputpath; string className; int timeInterval; int po = 1; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("d://StockUpConfig.xml"); XmlNode xn = xmlDoc.SelectSingleNode("stockwarning"); XmlNodeList xnl = xn.ChildNodes; XmlNode xn3 = xmlDoc.SelectSingleNode("stockwarning/goodsclass"); XmlNodeList xn3l = xn3.ChildNodes; foreach (XmlNode xnf in xnl) { XmlElement xe = (XmlElement)xnf; XmlNodeList xnf1 = xe.ChildNodes; xmlChildNodes = xmlChildNodes + xnf.InnerText + "|"; } string[] cc = xmlChildNodes.Split('|'); filialeId = cc[1]; companyType = cc[2]; timeInterval = Convert.ToInt16(cc[3]); outputpath = cc[4]; dsn = cc[5]; endTime = DateTime.Now.Date.ToShortDateString(); startTime = DateTime.Now.AddDays(-timeInterval).ToShortDateString(); SqlConnection connection = new SqlConnection(dsn); connection.Open(); foreach (XmlNode xn3f in xn3l) { XmlNode xn2 = xmlDoc.SelectSingleNode("stockwarning/goodsclass/group" + po); XmlNodeList xn2l = xn2.ChildNodes; XmlElement xe = (XmlElement)xn3f; className = xe.GetAttribute("classname"); po++; StringBuilder sb = new StringBuilder(); sb.Append(@"<?xml version=""1.0"" encoding=""utf-8"" ?>"); sb.Append("<root>"); int runTime = 3; int curRun = 0; foreach (XmlNode xn2f in xn2l) { classId = xn2f.InnerText; SqlCommand command = connection.CreateCommand(); //connection.ConnectionTimeout = 355; command.CommandText = "P_Raifei_GetClassGoodsStockUp"; command.CommandType = CommandType.StoredProcedure; SqlParameter param1 = new SqlParameter("@ClassId", classId); SqlParameter param2 = new SqlParameter("@FilialeId", filialeId); SqlParameter param3 = new SqlParameter("@StartTime", startTime); SqlParameter param4 = new SqlParameter("@EndTime", endTime); SqlParameter param5 = new SqlParameter("@CompanyType", companyType); command.Parameters.Add(param1); command.Parameters.Add(param2); command.Parameters.Add(param3); command.Parameters.Add(param4); command.Parameters.Add(param5); while (curRun++ < runTime) { try { SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { sb.Append("<goods>"); sb.AppendFormat("<goodsid>{0}</goodsid>", reader["goodsid"]); sb.AppendFormat("<goodsname>{0}</goodsname>", reader["goodsname"]); sb.AppendFormat("<specification>{0}</specification>", reader["specification"]); sb.AppendFormat("<NonceFilialeGoodsStock>{0}</NonceFilialeGoodsStock>", reader["NonceFilialeGoodsStock"]); sb.AppendFormat("<NonceRequest>{0}</NonceRequest>", reader["NonceRequest"]); sb.AppendFormat("<NonceGoodsStock>{0}</NonceGoodsStock>", reader["NonceGoodsStock"]); sb.AppendFormat("<SalesNumber>{0}</SalesNumber>", reader["SalesNumber"]); sb.Append("</goods>"); } reader.Close(); break; } catch { } Thread.Sleep(2000); } } if (curRun == 3) { String filePath = "D://test.txt"; TextWriter tw = File.AppendText(filePath); tw.Write(classId + "查询超时" + "/n"); tw.Close(); } sb.Append("</root>"); StreamWriter fs = File.CreateText(outputpath + "StockUp" + DateTime.Now.Date.ToShortDateString() + "-" + className + ".xml"); fs.Write(sb.ToString()); fs.Close(); fs.Dispose(); } connection.Close(); } protected override void OnStop() { eventLog1.WriteEntry("In onStop."); } protected override void OnContinue() { eventLog1.WriteEntry("In OnContinue."); } } }