打印

[asp.net教程] 用c#把文件转换为xml 2

用c#把文件转换为xml 2

/**//// <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("加载成功");
     }
   
    }
   
   }
  }
  http://www.cnblogs.com/skylaugh/archive/2006/12/18/595654.html

TOP

返回顶部
AYBlue

Processed in 0.052855 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 11:51 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP