用C#把檔案轉換為XML的代碼

來源:互聯網
上載者:User

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("載入成功");
   }

  }

 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.