首页 > 开发 > XML > 正文

用C#把文件转换为XML

2024-09-05 20:55:54
字体:
来源:转载
供稿:网友

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.io;
using system.xml;
namespace mywindows
{
 /**//// <summary>
 /// 这个示例演示如何把office文件编码为xml文件以及如何把生成的xml文件转换成office文件
 /// 把文件转换成xml格式,然后就可以用web服务,.net remoting,winsock等传送了(其中后两者可以不转换也可以传送)
 /// xml解决了在多层架构中数据传输的问题,比如说在客户端可以用web服务获取服务器端的office文件,修改后再回传给服务器
 /// 只要把文件转换成xml格式,便有好多方案可以使用了,而xml具有平台无关性,你可以在服务端用.net用发布web服务,然后客户端用
 /// java写一段applit小程序来处理发送过来的文件,当然我举的例子几乎没有任何显示意义,它却给了我们不少的启示.
 /// 另外如果你的解决方案是基于多平台的,那么他们之间的交互最好不要用远程应用程序接口调用(rpc),应该尽量用基于文档的交互,
 /// 比如说.net下的msmq,j2ee的jmq.
 ///
 /// 示例中设计到好多的类,我并没有在所有的地方做过多注释,有不明白的地方请参阅msdn,这是偶第一个windows程序,有不对的地方
 /// 欢迎各位指导
 /// </summary>
 public class form1 : system.windows.forms.form
 {

  /**//// <summary>
  /// 声明四个button,一个openfiledialog,一个savefiledialog,以及两个xmldocument
  /// </summary>
  private system.windows.forms.button button1;
  private system.windows.forms.button button2;
  private system.windows.forms.openfiledialog openfiledialog1;
  private system.windows.forms.savefiledialog savefiledialog1;
  private system.windows.forms.button button3;
  private system.windows.forms.button button4;
  private system.xml.xmldocument mxmldoc;
  private system.xml.xmldocument doc;
  private system.componentmodel.container components = null;

  public form1()
  {
   //
   // windows 窗体设计器支持所必需的
   //
   initializecomponent();

   //
   // todo: 在 initializecomponent 调用后添加任何构造函数代码
   //
  }

  /**//// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.dispose();
    }
   }
   base.dispose( disposing );
  }

  windows 窗体设计器生成的代码#region windows 窗体设计器生成的代码
  /**//// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {
   this.button1 = new system.windows.forms.button();
   this.button2 = new system.windows.forms.button();
   this.openfiledialog1 = new system.windows.forms.openfiledialog();
   this.savefiledialog1 = new system.windows.forms.savefiledialog();
   this.button3 = new system.windows.forms.button();
   this.button4 = new system.windows.forms.button();
   this.suspendlayout();
   //
   // button1
   //
   this.button1.location = new system.drawing.point(96, 32);
   this.button1.name = "button1";
   this.button1.tabindex = 0;
   this.button1.text = "生成xml";
   this.button1.click += new system.eventhandler(this.button1_click);
   //
   // button2
   //
   this.button2.location = new system.drawing.point(96, 80);
   this.button2.name = "button2";
   this.button2.tabindex = 1;
   this.button2.text = "生成doc";
   this.button2.click += new system.eventhandler(this.button2_click);
   //
   // button3
   //
   this.button3.location = new system.drawing.point(8, 32);
   this.button3.name = "button3";
   this.button3.tabindex = 2;
   this.button3.text = "加载doc";
   this.button3.click += new system.eventhandler(this.button3_click);
   //
   // button4
   //
   this.button4.location = new system.drawing.point(8, 80);
   this.button4.name = "button4";
   this.button4.tabindex = 3;
   this.button4.text = "加载xml";
   this.button4.click += new system.eventhandler(this.button4_click);
   //
   // form1
   //
   this.autoscalebasesize = new system.drawing.size(6, 14);
   this.clientsize = new system.drawing.size(184, 141);
   this.controls.add(this.button4);
   this.controls.add(this.button3);
   this.controls.add(this.button2);
   this.controls.add(this.button1);
   this.name = "form1";
   this.text = "form1";
   this.resumelayout(false);
   //
   //手工注册一下load和closed事件
   //
   this.load += new system.eventhandler(this.form1_load);
   this.closed += new system.eventhandler(this.form1_closed);

  }
  #endregion

  /**//// <summary>
  /// 从这个入口启动窗体
  /// </summary>
  static void main()
  {
   application.run(new form1());
  }
  /**//// <summary>
  /// 把加载的office文件转换为xml文件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button1_click(object sender, system.eventargs e)
  {
   savefiledialog1.filter = "xml 文件|*.xml";//设置打开对话框的文件过滤条件
   savefiledialog1.title = "保存成 xml 文件";//设置打开对话框的标题
   savefiledialog1.filename="";
   savefiledialog1.showdialog();//打开对话框

   if(savefiledialog1.filename != "")//检测用户是否输入了保存文件名
   {
    mxmldoc.save(savefiledialog1.filename);//用私有对象mxmldoc保存文件,mxmldoc在前面声明过
    messagebox.show("保存成功");
   }
  }

  /**//// <summary>
  /// 把加载的xml文件转换为office文件
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button2_click(object sender, system.eventargs e)
  {
   //从私有对象dox里选取me节点,这里的一些对xml对象的操作详细说明可以参考msdn以获取更多信息
   xmlnode node=doc.documentelement .selectsinglenode("me") ;
   xmlelement ele=(xmlelement)node;//获取一个xml元素
   string pic=ele.getattribute ("aa");//获取ele元素的aa属性并报讯在一个临时字符串变量pic

   byte[] bytes=convert.frombase64string (pic);//声明一个byte[]用来存放base64解码转换过来的数据流
  
   //从保存对话框里获取文件保存地址
   savefiledialog1.filter = "office documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
   savefiledialog1.title = "保存成 office 文件";
   savefiledialog1.filename="";
   savefiledialog1.showdialog();

   if(savefiledialog1.filename != "")
   {
    //创建文件流并保存
    filestream outfile=new system.io .filestream (savefiledialog1.filename,system.io.filemode.createnew);
    outfile.write(bytes,0,(int)bytes.length );
    messagebox.show("保存成功");
   }

  }

  /**//// <summary>
  /// 加载窗口时的一些初始化行为
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public void form1_load(object sender, system.eventargs e)
  {
   messagebox.show("欢迎使用蛙蛙牌文档转换器");
  }
  /**//// <summary>
  /// 卸载窗体时把临时变量全部释放
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  public void form1_closed(object sender, system.eventargs e)
  {
   mxmldoc=null;
   doc=null;
  }
  /**//// <summary>
  /// 加载office文件并编码序列花为一个xmldocument变量
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button3_click(object sender, system.eventargs e)
  {
   string strfilename;
   openfiledialog1.filter = "office documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;
   openfiledialog1.filterindex = 1;
   openfiledialog1.filename = "";
   openfiledialog1.showdialog();
   strfilename = openfiledialog1.filename;
   if(strfilename.length != 0)
   {
    system.io.filestream infile=new filestream(strfilename,system.io.filemode.open,system.io.fileaccess.read);
    byte[] binarydata=new byte [infile.length];
    infile.read(binarydata, 0,(int)infile.length);
    string mstr=convert.tobase64string(binarydata);
    string hh=mstr;
    mxmldoc=new system.xml.xmldocument();
 
    mstr=string.format ("<wawa><me aa=/"{0}/"/></wawa>",mstr);
    mxmldoc.loadxml( mstr);
    messagebox.show("加载成功");
   }

  }
  /**//// <summary>
  /// 加载xml文件到私有对象dox
  /// </summary>
  /// <param name="sender"></param>
  /// <param name="e"></param>
  private void button4_click(object sender, system.eventargs e)
  {
   string strfilename;
   openfiledialog1.filter = "xml 文件|*.xml" ;
   openfiledialog1.filterindex = 1;
   openfiledialog1.filename = "";
   openfiledialog1.showdialog();
   strfilename = openfiledialog1.filename;
   //if the user does not cancel, open the document.
   if(strfilename.length != 0)
   {
    doc=new xmldocument();
    doc.load(strfilename);
    messagebox.show("加载成功");
   }

  }

 }
}

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表