void DoExportXmlFile(string strOutputFileName) { string strError = ""; FileStream outputfile = null; XmlTextWriter writer = null; if (textBox_dbPath.Text == "") { MessageBox.Show(this, "尚未选择源库..."); return; } ResPath respath = new ResPath(textBox_dbPath.Text); channel = this.Channels.GetChannel(respath.Url); string strDbName = respath.Path; if (strOutputFileName != null && strOutputFileName != "") { // 探测文件是否存在 FileInfo fi = new FileInfo(strOutputFileName); if (fi.Exists == true && fi.Length > 0) { DialogResult result = MessageBox.Show(this, "文件 '" + strOutputFileName + "' 已存在,是否覆盖?\r\n\r\n--------------------\r\n注:(是)覆盖 (否)中断处理", "dp2batch", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (result != DialogResult.Yes) { strError = "放弃处理..."; goto ERROR1; } } // 打开文件 outputfile = File.Create( strOutputFileName); writer = new XmlTextWriter(outputfile, Encoding.UTF8); writer.Formatting = Formatting.Indented; writer.Indentation = 4; } try { Int64 nStart; Int64 nEnd; Int64 nCur; bool bAsc = GetDirection(out nStart, out nEnd); // 设置进度条范围 Int64 nMax = nEnd - nStart; if (nMax < 0) nMax *= -1; nMax ++; ProgressRatio = nMax / 10000; if (ProgressRatio < 1.0) ProgressRatio = 1.0; progressBar_main.Minimum = 0; progressBar_main.Maximum = (int)(nMax/ProgressRatio); progressBar_main.Value = 0; bool bFirst = true; // 是否为第一次取记录 string strID = this.textBox_startNo.Text; stop.Initial(new Delegate_doStop(this.DoStop), "正在导出数据"); stop.BeginLoop(); EnableControls(false); if (writer != null) { writer.WriteStartDocument(); writer.WriteStartElement("dprms","collection",DpNs.dprms); //writer.WriteStartElement("collection"); //writer.WriteAttributeString("xmlns:marc", // "http://www.loc.gov/MARC21/slim"); } // 循环 for(;;) { Application.DoEvents(); // 出让界面控制权 if (stop.State != 0) { strError = "用户中断"; goto ERROR1; } string strStyle = ""; if (outputfile != null) strStyle = "data,content,timestamp,outputpath"; else strStyle = "timestamp,outputpath"; // 优化 if (bFirst == true) strStyle += ""; else { if (bAsc == true) strStyle += ",next"; else strStyle += ",prev"; } string strPath = strDbName + "/" + strID; string strXmlBody = ""; string strMetaData = ""; byte[] baOutputTimeStamp = null; string strOutputPath = ""; bool bFoundRecord = false; // 获得资源 // return: // -1 出错。具体出错原因在this.ErrorCode中。this.ErrorInfo中有出错信息。 // 0 成功 long lRet = channel.GetRes(strPath, strStyle, out strXmlBody, out strMetaData, out baOutputTimeStamp, out strOutputPath, out strError); if (lRet == -1) { if (channel.ErrorCode == ChannelErrorCode.NotFound) { if (checkBox_forceLoop.Checked == true && bFirst == true) { AutoCloseMessageBox.Show(this, "记录 " + strID + " 不存在。\r\n\r\n按 确认 继续。"); bFirst = false; goto CONTINUE; } else { if (bFirst == true) { strError = "记录 " + strID + " 不存在。处理结束。"; } else { if (bAsc == true) strError = "记录 " + strID + " 是最末一条记录。处理结束。"; else strError = "记录 " + strID + " 是最前一条记录。处理结束。"; } } } else if (channel.ErrorCode == ChannelErrorCode.EmptyRecord) { bFirst = false; bFoundRecord = false; // 把id解析出来 strID = ResPath.GetRecordId(strOutputPath); goto CONTINUE; } goto ERROR1; } bFirst = false; bFoundRecord = true; // 把id解析出来 strID = ResPath.GetRecordId(strOutputPath); CONTINUE: stop.SetMessage(strID); // 是否超过循环范围 try { nCur = Convert.ToInt64(strID); } catch { // ??? nCur = 0; } if (bAsc == true && nCur > nEnd) break; if (bAsc == false && nCur < nEnd) break; if (bFoundRecord == true && writer != null) { // 写磁盘 XmlDocument dom = new XmlDocument(); try { dom.LoadXml(strXmlBody); ResPath respathtemp = new ResPath(); respathtemp.Url = channel.Url; respathtemp.Path = strOutputPath; // DomUtil.SetAttr(dom.DocumentElement, "xmlns:dprms", DpNs.dprms); // 给根元素设置几个参数 DomUtil.SetAttr(dom.DocumentElement, "path", DpNs.dprms, respathtemp.FullPath); DomUtil.SetAttr(dom.DocumentElement, "timestamp", DpNs.dprms, ByteArray.GetHexTimeStampString(baOutputTimeStamp)); // DomUtil.SetAttr(dom.DocumentElement, "xmlns:marc", null); dom.DocumentElement.WriteTo(writer); } catch (Exception ex) { strError = ex.Message; // 询问是否继续 goto ERROR1; } /* if (nRet == -1) { // 询问是否继续 goto ERROR1; } */ } // 删除 if (checkBox_export_delete.Checked == true) { byte [] baOutputTimeStamp1 = null; strPath = strOutputPath; // 得到实际的路径 lRet = channel.DoDeleteRecord( strPath, baOutputTimeStamp, out baOutputTimeStamp1, out strError); if (lRet == -1) { // 询问是否继续 goto ERROR1; } } if (bAsc == true) { progressBar_main.Value = (int)((nCur-nStart + 1)/ProgressRatio); } else { // ? progressBar_main.Value = (int)((nStart-nCur + 1)/ProgressRatio); } // 对已经作过的进行判断 if (bAsc == true && nCur >= nEnd) break; if (bAsc == false && nCur <= nEnd) break; } stop.EndLoop(); stop.Initial(null, ""); EnableControls(true); } finally { if (writer != null) { writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); writer = null; } if (outputfile != null) { outputfile.Close(); outputfile = null; } } END1: channel = null; if (checkBox_export_delete.Checked == true) MessageBox.Show(this, "数据导出和删除完成。"); else MessageBox.Show(this, "数据导出完成。"); return; ERROR1: stop.EndLoop(); stop.Initial(null, ""); EnableControls(true); channel = null; MessageBox.Show(this, strError); return; }