用C#把文件转换为XML

简介:
None.gif using System;
None.gif using System.Drawing;
None.gif using System.Collections;
None.gif using System.ComponentModel;
None.gif using System.Windows.Forms;
None.gif using System.IO;
None.gif using System.Xml;
None.gif namespace MyWindows
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif /**//// <summary>
InBlock.gif 
/// 这个示例演示如何把Office文件编码为xml文件以及如何把生成的xml文件转换成Office文件
InBlock.gif 
/// 把文件转换成xml格式,然后就可以用web服务,.NET Remoting,WinSock等传送了(其中后两者可以不转换也可以传送)
InBlock.gif 
/// xml解决了在多层架构中数据传输的问题,比如说在客户端可以用Web服务获取服务器端的office文件,修改后再回传给服务器
InBlock.gif 
/// 只要把文件转换成xml格式,便有好多方案可以使用了,而xml具有平台无关性,你可以在服务端用.net用发布web服务,然后客户端用
InBlock.gif 
/// Java写一段applit小程序来处理发送过来的文件,当然我举的例子几乎没有任何显示意义,它却给了我们不少的启示.
InBlock.gif 
/// 另外如果你的解决方案是基于多平台的,那么他们之间的交互最好不要用远程应用程序接口调用(RPC),应该尽量用基于文档的交互,
InBlock.gif 
/// 比如说.net下的MSMQ,j2ee的JMQ.
InBlock.gif 
///
InBlock.gif 
/// 示例中设计到好多的类,我并没有在所有的地方做过多注释,有不明白的地方请参阅MSDN,这是偶第一个windows程序,有不对的地方
InBlock.gif 
/// 欢迎各位指导
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 声明四个Button,一个OpenFileDialog,一个SaveFileDialog,以及两个XmlDocument
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private System.Windows.Forms.Button button1;
InBlock.gif  private System.Windows.Forms.Button button2;
InBlock.gif  private System.Windows.Forms.OpenFileDialog openFileDialog1;
InBlock.gif  private System.Windows.Forms.SaveFileDialog saveFileDialog1;
InBlock.gif  private System.Windows.Forms.Button button3;
InBlock.gif  private System.Windows.Forms.Button button4;
InBlock.gif  private System.Xml.XmlDocument mXmlDoc;
InBlock.gif  private System.Xml.XmlDocument doc;
InBlock.gif  private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif  public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   //
InBlock.gif   
// Windows 窗体设计器支持所必需的
InBlock.gif   
//
InBlock.gif
   InitializeComponent();
InBlock.gif
InBlock.gif   //
InBlock.gif   
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif   
//
ExpandedSubBlockEnd.gif
  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    if(components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif     components.Dispose();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   base.Dispose( disposing );
ExpandedSubBlockEnd.gif  }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif  Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif  
/// 此方法的内容。
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   this.button1 = new System.Windows.Forms.Button();
InBlock.gif   this.button2 = new System.Windows.Forms.Button();
InBlock.gif   this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
InBlock.gif   this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
InBlock.gif   this.button3 = new System.Windows.Forms.Button();
InBlock.gif   this.button4 = new System.Windows.Forms.Button();
InBlock.gif   this.SuspendLayout();
InBlock.gif   //
InBlock.gif   
// button1
InBlock.gif   
//
InBlock.gif
   this.button1.Location = new System.Drawing.Point(96, 32);
InBlock.gif   this.button1.Name = "button1";
InBlock.gif   this.button1.TabIndex = 0;
InBlock.gif   this.button1.Text = "生成xml";
InBlock.gif   this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif   //
InBlock.gif   
// button2
InBlock.gif   
//
InBlock.gif
   this.button2.Location = new System.Drawing.Point(96, 80);
InBlock.gif   this.button2.Name = "button2";
InBlock.gif   this.button2.TabIndex = 1;
InBlock.gif   this.button2.Text = "生成doc";
InBlock.gif   this.button2.Click += new System.EventHandler(this.button2_Click);
InBlock.gif   //
InBlock.gif   
// button3
InBlock.gif   
//
InBlock.gif
   this.button3.Location = new System.Drawing.Point(8, 32);
InBlock.gif   this.button3.Name = "button3";
InBlock.gif   this.button3.TabIndex = 2;
InBlock.gif   this.button3.Text = "加载doc";
InBlock.gif   this.button3.Click += new System.EventHandler(this.button3_Click);
InBlock.gif   //
InBlock.gif   
// button4
InBlock.gif   
//
InBlock.gif
   this.button4.Location = new System.Drawing.Point(8, 80);
InBlock.gif   this.button4.Name = "button4";
InBlock.gif   this.button4.TabIndex = 3;
InBlock.gif   this.button4.Text = "加载xml";
InBlock.gif   this.button4.Click += new System.EventHandler(this.button4_Click);
InBlock.gif   //
InBlock.gif   
// Form1
InBlock.gif   
//
InBlock.gif
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
InBlock.gif   this.ClientSize = new System.Drawing.Size(184, 141);
InBlock.gif   this.Controls.Add(this.button4);
InBlock.gif   this.Controls.Add(this.button3);
InBlock.gif   this.Controls.Add(this.button2);
InBlock.gif   this.Controls.Add(this.button1);
InBlock.gif   this.Name = "Form1";
InBlock.gif   this.Text = "Form1";
InBlock.gif   this.ResumeLayout(false);
InBlock.gif   //
InBlock.gif   
//手工注册一下Load和Closed事件
InBlock.gif   
//
InBlock.gif
   this.Load += new System.EventHandler(this.Form1_Load);
InBlock.gif   this.Closed += new System.EventHandler(this.Form1_Closed);
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  #endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 从这个入口启动窗体
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   Application.Run(new Form1());
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 把加载的Office文件转换为xml文件
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif  
/// <param name="e"></param>

InBlock.gif  private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   saveFileDialog1.Filter = "xml 文件|*.xml";//设置打开对话框的文件过滤条件
InBlock.gif
   saveFileDialog1.Title = "保存成 xml 文件";//设置打开对话框的标题
InBlock.gif
   saveFileDialog1.FileName="";
InBlock.gif   saveFileDialog1.ShowDialog();//打开对话框
InBlock.gif

InBlock.gif   if(saveFileDialog1.FileName != "")//检测用户是否输入了保存文件名
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    mXmlDoc.Save(saveFileDialog1.FileName);//用私有对象mXmlDoc保存文件,mXmlDoc在前面声明过
InBlock.gif
    MessageBox.Show("保存成功");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 把加载的xml文件转换为Office文件
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif  
/// <param name="e"></param>

InBlock.gif  private void button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   //从私有对象dox里选取me节点,这里的一些对xml对象的操作详细说明可以参考msdn以获取更多信息
InBlock.gif
   XmlNode node=doc.DocumentElement .SelectSingleNode("me") ;
InBlock.gif   XmlElement ele=(XmlElement)node;//获取一个xml元素
InBlock.gif
   string pic=ele.GetAttribute ("aa");//获取ele元素的aa属性并报讯在一个临时字符串变量pic
InBlock.gif

InBlock.gif   byte[] bytes=Convert.FromBase64String (pic);//声明一个byte[]用来存放Base64解码转换过来的数据流
InBlock.gif  
InBlock.gif   
//从保存对话框里获取文件保存地址
InBlock.gif
   saveFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
InBlock.gif   saveFileDialog1.Title = "保存成 office 文件";
InBlock.gif   saveFileDialog1.FileName="";
InBlock.gif   saveFileDialog1.ShowDialog();
InBlock.gif
InBlock.gif   if(saveFileDialog1.FileName != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    //创建文件流并保存
InBlock.gif
    FileStream outfile=new System.IO .FileStream (saveFileDialog1.FileName,System.IO.FileMode.CreateNew);
InBlock.gif    outfile.Write(bytes,0,(int)bytes.Length );
InBlock.gif    MessageBox.Show("保存成功");
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 加载窗口时的一些初始化行为
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif  
/// <param name="e"></param>

InBlock.gif  public void Form1_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   MessageBox.Show("欢迎使用蛙蛙牌文档转换器");
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 卸载窗体时把临时变量全部释放
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif  
/// <param name="e"></param>

InBlock.gif  public void Form1_Closed(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   mXmlDoc=null;
InBlock.gif   doc=null;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 加载office文件并编码序列花为一个XmlDocument变量
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif  
/// <param name="e"></param>

InBlock.gif  private void button3_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   string strFileName;
InBlock.gif   openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;
InBlock.gif   openFileDialog1.FilterIndex = 1;
InBlock.gif   openFileDialog1.FileName = "";
InBlock.gif   openFileDialog1.ShowDialog();
InBlock.gif   strFileName = openFileDialog1.FileName;
InBlock.gif   if(strFileName.Length != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    System.IO.FileStream inFile=new FileStream(strFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
InBlock.gif    byte[] binaryData=new byte [inFile.Length];
InBlock.gif    inFile.Read(binaryData, 0,(int)inFile.Length);
InBlock.gif    string mStr=Convert.ToBase64String(binaryData);
InBlock.gif    string hh=mStr;
InBlock.gif    mXmlDoc=new System.Xml.XmlDocument();
InBlock.gif 
ExpandedSubBlockStart.gifContractedSubBlock.gif    mStr=string.Format ("<wawa><me aa=\"dot.gif{0}\"/></wawa>",mStr);
InBlock.gif    mXmlDoc.LoadXml( mStr);
InBlock.gif    MessageBox.Show("加载成功");
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  /**//// <summary>
InBlock.gif  
/// 加载xml文件到私有对象dox
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif  
/// <param name="e"></param>

InBlock.gif  private void button4_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
InBlock.gif   string strFileName;
InBlock.gif   openFileDialog1.Filter = "xml 文件|*.xml" ;
InBlock.gif   openFileDialog1.FilterIndex = 1;
InBlock.gif   openFileDialog1.FileName = "";
InBlock.gif   openFileDialog1.ShowDialog();
InBlock.gif   strFileName = openFileDialog1.FileName;
InBlock.gif   //If the user does not cancel, open the document.
InBlock.gif
   if(strFileName.Length != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{
InBlock.gif    doc=new XmlDocument();
InBlock.gif    doc.Load(strFileName);
InBlock.gif    MessageBox.Show("加载成功");
ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2006/02/15/330898.html,如需转载请自行联系原作者
相关文章
|
3天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
11 0
|
15天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
16 0
|
17天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
1月前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
1月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
10 0
|
1月前
|
Kubernetes Cloud Native Java
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
39 1
|
1月前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
11 0
|
2月前
|
XML JavaScript 数据格式
打开 XML 文件报错 There is no Unicode byte order mark
打开 XML 文件报错 There is no Unicode byte order mark
43 0
|
2月前
|
XML 存储 数据格式
什么是 XML 文件的 Schema
什么是 XML 文件的 Schema
30 0
|
6天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
15 1