#region 导入flow文件
private void toolStripButton_import_Click(object sender, EventArgs e)
{
try
{
do_import();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
string flownamePath = "";
private void do_import()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "xml文件|*.xml";
ofd.ValidateNames = true;
ofd.CheckPathExists = true;
ofd.CheckFileExists = true;
string flowname = "";
if (ofd.ShowDialog() == DialogResult.OK)
{
flowname = ofd.FileName;
flownamePath = ofd.FileName;
string fileNameExt = flowname.Substring(flowname.LastIndexOf("\\") + 1); //获取文件名,不带路径
string flowid = fileNameExt.Substring(0, fileNameExt.LastIndexOf("."));
string xml = System.IO.File.ReadAllText(@flownamePath, Encoding.UTF8);
do_loadxml(flowid, xml);
}
else
return;
}
private void do_loadxml(string flowname, string xml)
{
string err = "";
LockWindowUpdate(this.Handle);
try
{
TabPage tb = new TabPage();
tb.Text = "正在加载";
tabControl1.TabPages.Add(tb);
tabControl1.SelectedIndex = tabControl1.TabPages.Count - 1;
ESBFlow flow = new ESBFlow();
tb.Controls.Add(flow);
flow.Dock = DockStyle.Fill;
if (xml != "")
{
flow.editor.loadxml(xml);
tb.Text = flow.editor.flowdoc.flow.name;
tb.Tag = flow.editor.flowdoc.flow.info.id;
}
else
{
MessageBox.Show("读取数据失败。");
tb.Text = flowname;
flow.new_flow();
flow.editor.flowdoc.flow.flowtype = FlowConst.flowtype_main;
}
set_btn_enabled();
}
catch (Exception ex)
{
err = ex.Message;
}
LockWindowUpdate(IntPtr.Zero);
set_btn_enabled();
if (err != "")
{
MessageBox.Show(err);
}
}
#endregion
#region 导出flow 文件
private void toolStripButton_export_Click(object sender, EventArgs e)
{
try
{
do_export();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void do_export()
{
//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.Filter = "xml文件|*.xml";
//设置默认文件类型显示顺序
sfd.FilterIndex = 1;
//保存对话框是否记忆上次打开的目录
sfd.RestoreDirectory = true;
//设置默认的文件名
ESBFlow f = get_curr_flow();
if (f == null)
{
MessageBox.Show("文件不存在,请先选择要导出的文件!");
return;
}
string xml = f.editor.save();
Dictionary<string, string> dictdata = new Dictionary<string, string>();
dictdata["flowid"] = f.editor.flowdoc.flow.info.id;
dictdata["flowname"] = f.editor.flowdoc.flow.name;
dictdata["flowtype"] = f.editor.flowdoc.flow.flowtype;
dictdata["flowdescription"] = f.editor.flowdoc.flow.description;
Dictionary<string, string> dict = new Dictionary<string, string>();
dict["data_json"] = Pub.make_json(dictdata);
dict["xml"] = xml;
set_dict_user(dict);
//sfd.DefaultFileName = "YourFileName";// in wpf is sfd.FileName = "YourFileName";
sfd.FileName = f.editor.flowdoc.flow.info.id;
//点了保存按钮进入
if (sfd.ShowDialog() == DialogResult.OK)
{
string localFilePath = sfd.FileName.ToString(); //获得文件路径
// string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径
System.IO.File.WriteAllText(localFilePath, xml, Encoding.UTF8);
//判断文件的存在
if (System.IO.File.Exists(localFilePath))
{
//存在文件
MessageBox.Show("导出成功!");
}
else
{
//不存在文件
MessageBox.Show("导出失败!");
}
}
}
#endregion
xml解析:
public static FlowDoc loaddoc(string xml)
{
if (string.IsNullOrEmpty(xml))
{
return new FlowDoc();
}
StringReader sr = new StringReader(xml);
XmlSerializer xmlserilize = new XmlSerializer(typeof(FlowDoc), get_extraTypes());
FlowDoc obj = (FlowDoc)xmlserilize.Deserialize(sr);
return obj;
}
来源:oschina
链接:https://my.oschina.net/8824/blog/3207674