• 网络学院
  • 新手学堂
  • 操作系统
  • 网络技术
  • 软件应用
  • 办公软件
  • 编程技术
  • 网站架设
  • 数据库类
  • 平面设计
  • 多媒体类
  • 游戏资讯
  • 教学论文
  • 认证考试
用C#把文件转换为XML
广告位
  站点:
  • 首 页
  • 最新软件
  • 最新文章
  • 国内软件
  • 国外软件
  • 汉化软件
  • 源码下载
  • 字体下载
用C#把文件转换为XML
软件发布 用C#把文件转换为XML
网络软件 系统工具 应用软件 联络聊天 图形图像 多媒体类 行业软件 游戏娱乐 编程开发 安全相关 教育教学 数码软件
热门软件: QQ 瑞星 pplive e话通 木马克星 千千静听 office2000 五笔字根 Photoshop 视频分割
返回首页 | 文章首页 | 编程技术 | C#教程 | 用C#把文件转换为XML

用C#把文件转换为XML

 

添加时间: 2007-7-15 4:08:39  作者: C#教程  阅读次数:62   来源: http://www.d9soft.com

 

 

       

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 );
  }

  #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("加载成功");
   }

  }

 }
}

 

 

 

 

上下文章:

 

上一篇文章: 如何得到硬盘序列号[C#] 下一篇文章: C#中用API实现MP3等音频文件的播放类

相关文章:

  • 卑鄙病毒:加密你的文件勒索你的钱
  • 端午节忠告:要警惕FLASH祝福文件里的包藏祸心
  • Oracle联机日志文件丢失处理方法
  • servlet实现oracle中读出文件并显示
  • ORACLE控制文件的重建

相关软件:

  • 木马文件粉碎机 V3.0
  • 3GP、MP4视频转换精灵 V1.8.3
  • 图片转换器 2.1
  • 单位转换手册 V1.7 先进版
  • Office 2003 简繁转换加载宏
  • 超级秘密文件夹 5.81

 

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • ASP源码
  • PHP源码
  • Net源码
  • JSP 源码

编程技术分类导航

  • ASP & ASP.NET教程
  • PHP教程
  • JSP教程
  • C/C++教程
  • VB & VB.NET教程
  • VC教程
  • Delphi教程
  • BCB教程
  • VFP教程
  • PB教程
  • JAVA教程
  • XML教程
  • C#教程
  • CGI教程

本类经典文章推荐

  • ADO.NET的开发场景及传统ADO的处理
  • 利用Visual C#实现Window管道技术
  • C#取得汉字的拼音的首字母
  • 使用C#编写DES加密程序的framework
  • Visual C#编写3D游戏框架示例
  • 用C#和本地Windows API操纵系统菜...
  • 在C#程序设计中使用Win32类库
  • Visual C#中调用Windows服务初探
  • 如何在C#的WinForm中制作饼状图和...
  • C#中实现DataGrid双向排序

C#教程阅读排行

  • 如何在C#的WinForm中制作饼状图和...
  • 浅析C#中图形编程
  • 用C#和本地Windows API操纵系统菜...
  • 彻底剖析C# 2.0泛型类的创建和使用
  • Visual C#编写3D游戏框架示例
  • 使用C#编写DES加密程序的framework
  • Visual C# 2005实现控件中捕获按键
  • Visual C#中调用Windows服务初探
  • C#如何在Form中嵌入并且操作Excel...
  • C#中利用mediaplayer打造mp3播放器

编程技术阅读总排行

  • VB入门教程之一
  • 第二章 PowerBuilder 入门之创建新...
  • Java连接数据库实例
  • 第一章 什么是PowerBuilder
  • VC++之List Box/Check List Box控...
  • VC++ List Ctrl控件
  • VC++ Combo Box/Combo Box Ex控件
  • 学C++不得不看的一篇文章
  • VB入门教程之二
  • VC++之Button控件

广告位置

字母检索 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 回到顶部

关于我们 | 版权声明 | 免责条款 | 广告联系 | 软件发布 | 下载帮助 | 下载排行 | 网站地图 | 特别鸣谢 | 友情连接

copyright; 2005-2008 D9soft.com 第九软件网 版权所有