用C#把文件转换为XML的代码

时间:2022-06-03 15:15:30
  1. using System;  
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.ComponentModel;  
  5. using System.Windows.Forms;  
  6. using System.IO;  
  7. using System.Xml;   
  8. namespace MyWindows  
  9. {  
  10.  /**//// <summary>  
  11.  /// 这个示例演示如何把Office文件编码为xml文件以及如何把生成的xml文件转换成Office文件  
  12.  /// 把文件转换成xml格式,然后就可以用web服务,.NET Remoting,WinSock等传送了(其中后两者可以不转换也可以传送)  
  13.  /// xml解决了在多层架构中数据传输的问题,比如说在客户端可以用Web服务获取服务器端的office文件,修改后再回传给服务器  
  14.  /// 只要把文件转换成xml格式,便有好多方案可以使用了,而xml具有平台无关性,你可以在服务端用.net用发布web服务,然后客户端用  
  15.  /// Java写一段applit小程序来处理发送过来的文件,当然我举的例子几乎没有任何显示意义,它却给了我们不少的启示.  
  16.  /// 另外如果你的解决方案是基于多平台的,那么他们之间的交互最好不要用远程应用程序接口调用(RPC),应该尽量用基于文档的交互,  
  17.  /// 比如说.net下的MSMQ,j2ee的JMQ.  
  18.  ///   
  19.  /// 示例中设计到好多的类,我并没有在所有的地方做过多注释,有不明白的地方请参阅MSDN,这是偶第一个windows程序,有不对的地方  
  20.  /// 欢迎各位指导   
  21.  /// </summary>  
  22.  public class Form1 : System.Windows.Forms.Form  
  23.  {  
  24.  
  25.   /**//// <summary>  
  26.   /// 声明四个Button,一个OpenFileDialog,一个SaveFileDialog,以及两个XmlDocument  
  27.   /// </summary>  
  28.   private System.Windows.Forms.Button button1;  
  29.   private System.Windows.Forms.Button button2;  
  30.   private System.Windows.Forms.OpenFileDialog openFileDialog1;  
  31.   private System.Windows.Forms.SaveFileDialog saveFileDialog1;  
  32.   private System.Windows.Forms.Button button3;  
  33.   private System.Windows.Forms.Button button4;  
  34.   private System.Xml.XmlDocument mXmlDoc;  
  35.   private System.Xml.XmlDocument doc;  
  36.   private System.ComponentModel.Container components = null;  
  37.  
  38.   public Form1()  
  39.   {  
  40.    //  
  41.    // Windows 窗体设计器支持所必需的  
  42.    //  
  43.    InitializeComponent();  
  44.  
  45.    //  
  46.    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码  
  47.    //  
  48.   }  
  49.  
  50.   /**//// <summary>  
  51.   /// 清理所有正在使用的资源。  
  52.   /// </summary>  
  53.   protected override void Dispose( bool disposing )  
  54.   {  
  55.    if( disposing )  
  56.    {  
  57.     if(components != null)  
  58.     {  
  59.      components.Dispose();  
  60.     }  
  61.    }  
  62.    base.Dispose( disposing );  
  63.   }  
  64.  
  65.   Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码  
  66.   /**//// <summary>  
  67.   /// 设计器支持所需的方法 - 不要使用代码编辑器修改  
  68.   /// 此方法的内容。  
  69.   /// </summary>  
  70.   private void InitializeComponent()  
  71.   {  
  72.    this.button1 = new System.Windows.Forms.Button();  
  73.    this.button2 = new System.Windows.Forms.Button();  
  74.    this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();  
  75.    this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();  
  76.    this.button3 = new System.Windows.Forms.Button();  
  77.    this.button4 = new System.Windows.Forms.Button();  
  78.    this.SuspendLayout();  
  79.    //   
  80.    // button1  
  81.    //   
  82.    this.button1.Location = new System.Drawing.Point(96, 32);  
  83.    this.button1.Name = "button1";  
  84.    this.button1.TabIndex = 0;  
  85.    this.button1.Text = "生成xml";  
  86.    this.button1.Click += new System.EventHandler(this.button1_Click);  
  87.    //   
  88.    // button2  
  89.    //   
  90.    this.button2.Location = new System.Drawing.Point(96, 80);  
  91.    this.button2.Name = "button2";  
  92.    this.button2.TabIndex = 1;  
  93.    this.button2.Text = "生成doc";  
  94.    this.button2.Click += new System.EventHandler(this.button2_Click);  
  95.    //   
  96.    // button3  
  97.    //   
  98.    this.button3.Location = new System.Drawing.Point(8, 32);  
  99.    this.button3.Name = "button3";  
  100.    this.button3.TabIndex = 2;  
  101.    this.button3.Text = "加载doc";  
  102.    this.button3.Click += new System.EventHandler(this.button3_Click);  
  103.    //   
  104.    // button4  
  105.    //   
  106.    this.button4.Location = new System.Drawing.Point(8, 80);  
  107.    this.button4.Name = "button4";  
  108.    this.button4.TabIndex = 3;  
  109.    this.button4.Text = "加载xml";  
  110.    this.button4.Click += new System.EventHandler(this.button4_Click);  
  111.    //   
  112.    // Form1  
  113.    //   
  114.    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  
  115.    this.ClientSize = new System.Drawing.Size(184, 141);  
  116.    this.Controls.Add(this.button4);  
  117.    this.Controls.Add(this.button3);  
  118.    this.Controls.Add(this.button2);  
  119.    this.Controls.Add(this.button1);  
  120.    this.Name = "Form1";  
  121.    this.Text = "Form1";  
  122.    this.ResumeLayout(false);  
  123.    //  
  124.    //手工注册一下Load和Closed事件  
  125.    //  
  126.    this.Load += new System.EventHandler(this.Form1_Load);  
  127.    this.Closed += new System.EventHandler(this.Form1_Closed);  
  128.  
  129.   }  
  130.   #endregion  
  131.  
  132.   /**//// <summary>  
  133.   /// 从这个入口启动窗体   
  134.   /// </summary>  
  135.   static void Main()  
  136.   {  
  137.    Application.Run(new Form1());  
  138.   }  
  139.   /**//// <summary>  
  140.   /// 把加载的Office文件转换为xml文件  
  141.   /// </summary>  
  142.   /// <param name="sender"></param>  
  143.   /// <param name="e"></param>  
  144.   private void button1_Click(object sender, System.EventArgs e)  
  145.   {   
  146.    saveFileDialog1.Filter = "xml 文件|*.xml";//设置打开对话框的文件过滤条件  
  147.    saveFileDialog1.Title = "保存成 xml 文件";//设置打开对话框的标题  
  148.    saveFileDialog1.FileName="";  
  149.    saveFileDialog1.ShowDialog();//打开对话框  
  150.  
  151.    if(saveFileDialog1.FileName != "")//检测用户是否输入了保存文件名  
  152.    {  
  153.     mXmlDoc.Save(saveFileDialog1.FileName);//用私有对象mXmlDoc保存文件,mXmlDoc在前面声明过  
  154.     MessageBox.Show("保存成功");  
  155.    }   
  156.   }  
  157.  
  158.   /**//// <summary>  
  159.   /// 把加载的xml文件转换为Office文件  
  160.   /// </summary>  
  161.   /// <param name="sender"></param>  
  162.   /// <param name="e"></param>  
  163.   private void button2_Click(object sender, System.EventArgs e)  
  164.   {  
  165.    //从私有对象dox里选取me节点,这里的一些对xml对象的操作详细说明可以参考msdn以获取更多信息  
  166.    XmlNode node=doc.DocumentElement .SelectSingleNode("me") ;  
  167.    XmlElement ele=(XmlElement)node;//获取一个xml元素  
  168.    string pic=ele.GetAttribute ("aa");//获取ele元素的aa属性并报讯在一个临时字符串变量pic  
  169.  
  170.    byte[] bytes=Convert.FromBase64String (pic);//声明一个byte[]用来存放Base64解码转换过来的数据流  
  171.     
  172.    //从保存对话框里获取文件保存地址  
  173.    saveFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";  
  174.    saveFileDialog1.Title = "保存成 office 文件";  
  175.    saveFileDialog1.FileName="";  
  176.    saveFileDialog1.ShowDialog();  
  177.  
  178.    if(saveFileDialog1.FileName != "")  
  179.    {  
  180.     //创建文件流并保存  
  181.     FileStream outfile=new System.IO .FileStream (saveFileDialog1.FileName,System.IO.FileMode.CreateNew);  
  182.     outfile.Write(bytes,0,(int)bytes.Length );  
  183.     MessageBox.Show("保存成功");  
  184.    }  
  185.  
  186.   }  
  187.  
  188.   /**//// <summary>  
  189.   /// 加载窗口时的一些初始化行为  
  190.   /// </summary>  
  191.   /// <param name="sender"></param>  
  192.   /// <param name="e"></param>  
  193.   public void Form1_Load(object sender, System.EventArgs e)  
  194.   {  
  195.    MessageBox.Show("欢迎使用蛙蛙牌文档转换器");  
  196.   }  
  197.   /**//// <summary>  
  198.   /// 卸载窗体时把临时变量全部释放  
  199.   /// </summary>  
  200.   /// <param name="sender"></param>  
  201.   /// <param name="e"></param>  
  202.   public void Form1_Closed(object sender, System.EventArgs e)  
  203.   {  
  204.    mXmlDoc=null;  
  205.    doc=null;  
  206.   }  
  207.   /**//// <summary>  
  208.   /// 加载office文件并编码序列花为一个XmlDocument变量  
  209.   /// </summary>  
  210.   /// <param name="sender"></param>  
  211.   /// <param name="e"></param>  
  212.   private void button3_Click(object sender, System.EventArgs e)  
  213.   {  
  214.    string strFileName;  
  215.    openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;  
  216.    openFileDialog1.FilterIndex = 1;  
  217.    openFileDialog1.FileName = "";  
  218.    openFileDialog1.ShowDialog();  
  219.    strFileName = openFileDialog1.FileName;  
  220.    if(strFileName.Length != 0)  
  221.    {  
  222.     System.IO.FileStream inFile=new FileStream(strFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);  
  223.     byte[] binaryData=new byte [inFile.Length];  
  224.     inFile.Read(binaryData, 0,(int)inFile.Length);  
  225.     string mStr=Convert.ToBase64String(binaryData);  
  226.     string hh=mStr;  
  227.     mXmlDoc=new System.Xml.XmlDocument();   
  228.    
  229.     mStr=string.Format ("<wawa><me aa=\"{0}\"/></wawa>",mStr);  
  230.     mXmlDoc.LoadXml( mStr);  
  231.     MessageBox.Show("加载成功");  
  232.    }   
  233.  
  234.   }  
  235.   /**//// <summary>  
  236.   /// 加载xml文件到私有对象dox  
  237.   /// </summary>  
  238.   /// <param name="sender"></param>  
  239.   /// <param name="e"></param>  
  240.   private void button4_Click(object sender, System.EventArgs e)  
  241.   {  
  242.    string strFileName;  
  243.    openFileDialog1.Filter = "xml 文件|*.xml" ;  
  244.    openFileDialog1.FilterIndex = 1;  
  245.    openFileDialog1.FileName = "";  
  246.    openFileDialog1.ShowDialog();  
  247.    strFileName = openFileDialog1.FileName;  
  248.    //If the user does not cancel, open the document.  
  249.    if(strFileName.Length != 0)  
  250.    {  
  251.     doc=new XmlDocument();  
  252.     doc.Load(strFileName);  
  253.     MessageBox.Show("加载成功");  
  254.    }  
  255.  
  256.   }  
  257.  
  258.  }  
  259. }