仰天一笑
昨日不悔,今日勿失,明日莫忧! —徐羽
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
::
241 随笔 :: 27 文章 :: 876 评论 :: 43 引用
用C#把文件转换为XML
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(
"
加载成功
"
);
}
}
}
}
posted on 2006-12-18 14:06
仰天一笑
阅读(820)
评论(2)
编辑
收藏
所属分类:
Javascript/Ajax/XML
评论
#1楼
2008-11-01 22:51
muzi[未注册用户]
转换的两段代码都不可以啊?
不能转换啊
加载虽然能够通过但是没有任何作用啊
没有显示啊
回复
引用
刷新评论
切换模板
发表评论
昵称:
[登录]
[注册]
主页:
邮箱:
(仅博主可见)
验证码:
看不清,换一个
评论内容:
登录
注册
[使用Ctrl+Enter键快速提交评论]
0
595654
导航:
网站首页
社区
新闻
博问
闪存
网摘
招聘
找找看
Google搜索
China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务
相关文章:
最新IT新闻:
19岁天才黑客发布首个iPhone 3GS破解软件
新浪邮箱大本营粉墨登场!Sina.cn开放注册
IE市场份额首次跌破60%
Google App Engine宕机6小时——云的安全在哪里?
微软新推社交网站Windows Live Planet
相关链接:
公告
一、版权归原创作者所有。
二、坚持挖掘互联网宝藏。
三、重在技术交流,共享知识。
四、技术文章包括asp.net、seo、ajax、css、javascript等。
Email:ansonxy@gmail.com
MSN:dingxi819@yahoo.com.cn
我的最新闪存
今天收获不小,要更加努力
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我参加的小组
WEB程序安全小组
我参与的团队
上海.NET俱乐部(0/0)
Ajax&Atlas技术团队(0/0)
软件项目管理团队(0/0)
.net企业应用开发(0/0)
我的标签
火车站售票点
(1)
上海火车站售票点
(1)
上海火车票预定送票公司
(1)
一票难求
(1)
海量查询
(1)
无限分级
(1)
树形结构数据查询
(1)
datatable
(1)
dataview
(1)
datatable和dataview
(1)
更多
随笔分类
(268)
ASP.Net-C#(75)
(rss)
CSS(1)
(rss)
DataBase(31)
(rss)
Javascript/Ajax/XML(49)
(rss)
产品专卖(4)
(rss)
电子商务
(rss)
古风古韵(10)
(rss)
谈天论地(31)
(rss)
网站建设(30)
(rss)
原创天地(37)
(rss)
文章分类
(27)
DotNet(12)
(rss)
DRM(5)
(rss)
Electronic Commerce(2)
(rss)
JavaScript(3)
(rss)
software engineering (5)
(rss)
相册
汇亚创意设计
上海汇亚科技公司
收藏夹
(202)
.Text(11)
(rss)
ADO.Net(4)
(rss)
Ajax/XML(10)
(rss)
ASP.Net(53)
(rss)
C#(6)
(rss)
DataBase(6)
(rss)
DataGrid(12)
(rss)
Html/Css(6)
(rss)
IIS(1)
(rss)
Script(24)
(rss)
搜索引擎(11)
(rss)
谈天论地(8)
(rss)
网站优化/推广(9)
(rss)
网址收集(23)
(rss)
项目管理(9)
(rss)
源代码(9)
(rss)
My Links
dudu
(rss)
jackei
(rss)
net366_汇亚科技
seo探索
公司BLOG
(rss)
海东
(rss)
黑梦SEO
汇亚创意设计
酷勤网
樂思蜀SEO博客
(rss)
李天平
(rss)
网络安全
兄弟blog
最新随笔
1. 几种常见的FTP软件的二进制设置说明
2. fckeditor在IE7下不显示
3. Div样式总结
4. 2003服务器 查IIS 挂马全过程(ARP挂马)
5. t-sql整理(转)
6. 防止卡巴被封的几点小常识供大家参考
7. SQLServer修改表所有者
8. onscroll事件导致flash叠影效果--setTimeout的妙用
9. Sql Server数据库被置疑后解决方法
10. [转]十二时辰养生法
11. 安装SQL SERVER2000提示注册表文件被挂起的解决方案
12. [转]科学思维-智慧国王故事
13. 23条心灵寄语献给在创业一线的兄弟姐妹
14. 余士维讲座中的十八个故事[转载]
15. DataTable 和 DataView 的理解
16. [推荐]无限分级数量查询优化
17. 一票难求-摘二片小诗已解胸口之闷
18. 上海火车站售票点
19. ERP项目管理12要点
20. ASP.NET常用代码
21. asp.net c#中对cookie的操作
22. 如何搭建企业局域网共享
23. SQL Sever安装问题
24. Asp.Net Web项目打包
25. 韩文数据库存取乱码解决方案
26. Seomoz年度Web2.0排名 中国网站无一入围
27. 数据库存取非简体中文出现乱码的解决
28. 商业网站设计主要原则
29. 网站策划,网站建设的重中之重
30. 软件项目实施规范小结
31. iis权限设置
32. 如何设置SQL Server 全文搜索
33. 批处理命令小集
34. 批量生成虚拟目录
35. 上海公交路线一览表
36. 抽闲破个案,放松一下(1)
37. IIS健全应用程序池设置
38. 《亮剑》李云龙原型之一:王近山中将
39. 如何清除木马--104种木马手工清除方法
40. 流媒体数字版权控制系统DRM
Google搜索
积分与排名
积分 - 509956
排名 - 57
最新评论
1. re: 上海长途汽车站客运站点及时刻表
请问从武汉到枣阳的车有那几个时间段
--梦想♂
2. re: onscroll事件导致flash叠影效果--setTimeout的妙用
这个方法我用过了,没有用呀。<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML...
--lanhai002
3. re: 管理者必读的四个小故事
您好,看您这篇文章,觉得很不错,不好意思没有经过您的同意我就将您的文章转我的网站上去了,如有不是还请谅解,谢谢!如果有什么事您可以发邮件到此邮箱macauley@163.com
--minmandy
4. re: [整理]asp.net 上传大文件解决方案
有没有AspnetUpload中dll的源代码,请发一份给我好吗,谢谢楼主!
lizhimin0310@163.com
--zhiminli
5. re: 数据库存取非简体中文出现乱码的解决
查了很多资料没有解决,楼主的帖子帮我解决了(第1和2条)
thx
--过客1
阅读排行榜
1. 上海长途汽车站客运站点及时刻表(44071)
2. ArrayList用法(26446)
3. 推荐--jQuery使用手册(24001)
4. 李白诗集(19503)
5. 推荐-高质量C++/C编程指南(林锐)(16136)
6. 经典推荐--.Net面试法宝(面试题收集)(15132)
7. 上海公交路线一览表(10120)
8. “SQL Server不存在或访问被拒绝”问题的解决(8523)
9. DataGrid中DropDownList的动态绑定和触发DropDownList事件(7660)
10. 远程连接SQL Server(7577)
11. [原]用window.location.href实现刷新另个框架页面(5637)
12. IIS启动不了(意外错误0x8ffe2740)(5244)
13. javascript实现datagrid客户端checkbox列的全选,反选 (5087)
14. window.close关闭窗口,不弹出系统提示,直接关闭(5048)
15. 征集佳句-精妙SQL语句收集(5003)
16. [整理]asp.net 上传大文件解决方案(4877)
17. Js+XML 操作(4787)
18. 关键字加亮--JS方法(4648)
19. location用法简单介绍(4546)
20. [强烈推荐]新手入门提速器,有了他,ASP.NET问题将迎刃而解(4297)
21. Sql Server数据库的备份和恢复措施(3948)
22. [转贴]Hello NHibernate(3793)
23. C# 中的类型转换(3729)
24. js事件列表(3665)
25. ASP.Net防止刷新自动触发事件的解决方案(3620)
26. 按比例缩放图片(3600)
27. 唐伯虎诗词(3440)
28. 抓取Web网页数据分析(3387)
29. ASP.NET 网站路径(3275)
30. [原]简易文本编辑器源码(3266)
31. 在C#中应用哈希表(Hashtable)(3208)
32. 不要让SQLServer自动转换害了你!(3197)
33. [原]Ajax简单客户登陆验证(3170)
34. 经典推荐—.NET相关最好东东(全球最新评价)(3164)
35. 界面设计测试规范(3160)
36. 满汉全席菜谱收藏版(3153)
37. [原]提交页面的定位--scrollIntoView的用法(3111)
38. DataGrid自动编号之黄金版(2989)
39. 如何搭建企业局域网共享(2850)
40. 正则表达式过滤HTML危险脚本(2755)
评论排行榜
1. [整理]asp.net 上传大文件解决方案(87)
2. 上海长途汽车站客运站点及时刻表(49)
3. 推荐--jQuery使用手册(39)
4. ArrayList用法(36)
5. 关键字加亮--JS方法(23)
6. 经典推荐--.Net面试法宝(面试题收集)(22)
7. 李白诗集(22)
8. “SQL Server不存在或访问被拒绝”问题的解决(21)
9. javascript实现datagrid客户端checkbox列的全选,反选 (18)
10. [推荐]测试你有没有经理头脑(17)