国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 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("加載成功");
   }

  }

 }
}

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 政和县| 阳新县| 汉阴县| 濉溪县| 夏邑县| 深圳市| 堆龙德庆县| 双柏县| 博爱县| 甘泉县| 张家口市| 五常市| 田林县| 贵州省| 高台县| 潼关县| 北流市| 绥棱县| 赤峰市| 佛山市| 刚察县| 红桥区| 天台县| 松江区| 江阴市| 筠连县| 元氏县| 甘德县| 韶关市| 繁昌县| 彰化县| 青铜峡市| 百色市| 普洱| 巴里| 永清县| 神农架林区| 金溪县| 天峨县| 沭阳县| 建湖县|