Пример #1
0
        private void SaveToXMLFile(string fileNameTemp, string message)
        {
            string strProcedureName =
                string.Format(
                    "{0}.{1}",
                    className,
                    MethodBase.GetCurrentMethod().Name);

            if (message != "")
            {
                try
                {
                    XmlDocument readXml = new XmlDocument();
                    readXml.LoadXml(message);

                    XmlNode node = readXml.SelectSingleNode("/Softland/Head/CorrelationID");
                    if (node == null)
                    {
                        OutputLog(
                            "消息报文头不标准,没有找到 [CorrelationID] 节点",
                            strProcedureName,
                            ToolTipIcon.Error);
                        return;
                    }
                    string correlationID = node.InnerText;

                    node = readXml.SelectSingleNode("/Softland/Head/UnixTime");
                    if (node == null)
                    {
                        OutputLog(
                            "消息报文头不标准,没有找到 [UnixTime] 节点",
                            strProcedureName,
                            ToolTipIcon.Error);
                        return;
                    }
                    DateTime msgTime = TimeParser.UnixToLocalTime(Convert.ToInt64(node.InnerText));

                    msgTime      = msgTime.AddSeconds(8 * 60 * 60);
                    fileNameTemp = fileNameTemp.Replace("[ExCode]", SysParams.Instance.ExCode);
                    fileNameTemp = fileNameTemp.Replace("[CorrelationID]", correlationID);
                    fileNameTemp = fileNameTemp.Replace("[DateTime]", msgTime.ToString("yyyyMMddHHmmss"));

                    string fileFullPathName =
                        string.Format(
                            @"{0}\{1}.xml.tmp",
                            SysParams.Instance.LocalFileSaveLocation,
                            fileNameTemp);
                    fileFullPathName.Replace(@"\\", @"\");

                    XmlNode nodeBody = readXml.SelectNodes("/Softland/Body")[0];

                    XmlDocument    writeXml = new XmlDocument();
                    XmlDeclaration xmldec   = writeXml.CreateXmlDeclaration("1.0", "utf-8", "yes");

                    XmlNode writeRootNode = null;
                    if (SysParams.Instance.XmlRootNodeName != "")
                    {
                        writeRootNode = writeXml.CreateElement(SysParams.Instance.XmlRootNodeName);
                    }
                    else
                    {
                        writeRootNode = writeXml.CreateElement("Root");
                    }

                    writeRootNode.InnerXml = nodeBody.InnerXml;
                    //writeXml.InsertAfter(writeRootNode, xmldec);
                    writeXml.AppendChild(xmldec);
                    writeXml.AppendChild(writeRootNode);

                    string fileName     = fileFullPathName;
                    string trueFileName = fileFullPathName.Substring(0, fileFullPathName.Length - 4);

                    //writeXml.Save(fileName);
                    var          utf8WithBom = new System.Text.UTF8Encoding(false); // 用true来指定包含bom
                    StreamWriter swr         = null;
                    try
                    {
                        //更新数据库状态不需要再打了

                        swr = new StreamWriter(fileName, false, utf8WithBom);
                        writeXml.Save(swr);
                        OutputLog(
                            string.Format(
                                "[{0}] 生成完毕!",
                                trueFileName),
                            strProcedureName,
                            ToolTipIcon.Info);
                    }
                    catch (Exception e)
                    {
                        OutputLog("生成文件错误:" + e.Message, strProcedureName, ToolTipIcon.Error);
                    }
                    finally
                    {
                        if (swr != null)
                        {
                            swr.Close();
                            swr.Dispose();
                        }
                    }
                    //重命名文件
                    File.Move(fileName, trueFileName);
                }
                catch (Exception error)
                {
                    OutputLog(error.Message, strProcedureName, ToolTipIcon.Error);
                }
            }
        }