仰天一笑
昨日不悔,今日勿失,明日莫忧! —徐羽
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
::
274 随笔 :: 27 文章 :: 1001 评论 :: 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
仰天一笑
阅读(1199)
评论(2)
编辑
收藏
评论
1358373
#1楼
2008-11-01 22:51
muzi[未注册用户]
转换的两段代码都不可以啊?
不能转换啊
加载虽然能够通过但是没有任何作用啊
没有显示啊
回复
引用
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
伊朗封杀Gmail和Facebook等互联网服务
·
分析称专利之争让谷歌苹果两败俱伤
·
Android平台发现新型手机病毒Rootsmart
·
HTC首款Android4.0手机大曝光
·
这是不是你期待的 iPad 3?
»
更多新闻...
最新知识库文章
:
·
高级编程语言的发展历程
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
公告
一、版权归原创作者所有;
二、坚持挖掘互联网宝藏;
三、重在技术交流,共享知识,愿交天下朋友;
四、技术文章包括asp.net、seo、ajax、css、javascript等。
Email:ansonxy@gmail.com
QQ:
943530498
MSN:
dingxi819@yahoo.com.cn
仰天一笑心灵成长博客
仰天一笑电子商务博客
仰天一笑新浪微博
昵称:
仰天一笑
园龄:
6年5个月
粉丝:
65
关注:
17
搜索
常用链接
我的随笔
我的评论
我的参与
最新评论
我的标签
最新随笔
1. 解决方案:Could not write to output file 'c:\windows\Microsoft.NET\Framework\........dll' -- '拒绝访问。 '
2. 一般情况下asp可以正常运行,但只要连接数据库就提示,Microsoft JET Database Engine 错误
3. 关于Asp.net无法写入输出文件的原因
4. 【推荐】Lazy Load, 延迟加载图片的 jQuery 插件
5. 中秋祝福
6. 纳兰性德诗词全集
7. 传统企业品牌转型电商的几大思考
8. World Wide Web Publishing服务无法启动提示:错误127 找不到指定程序/路径
9. ASP.NET(C#)常用数据加密和解密方法汇总
10. 数据加密技术
我的标签
数据加密技术
(3)
数据加密
(3)
数据加密算法
(1)
身份认证
(1)
信息认证技术
(1)
数字签名
(1)
密钥管理技术
(1)
C#常用加密方法汇总
(1)
对称加密
(1)
非对称加密
(1)
更多
随笔分类
(310)
ASP.Net-C#(85)
(rss)
CSS(1)
(rss)
DataBase(33)
(rss)
Javascript/Ajax/XML(51)
(rss)
产品专卖(4)
(rss)
电子商务(8)
(rss)
古风古韵(13)
(rss)
谈天论地(33)
(rss)
网站建设(36)
(rss)
项目管理(1)
(rss)
疑难杂症(2)
(rss)
原创天地(43)
(rss)
文章分类
(27)
DotNet(12)
(rss)
DRM(5)
(rss)
Electronic Commerce(2)
(rss)
JavaScript(3)
(rss)
software engineering (5)
(rss)
相册
汇亚创意设计
上海汇亚科技公司
My Links
dudu
(rss)
jackei
(rss)
net366_汇亚科技
seo探索
公司BLOG
(rss)
海东
(rss)
黑梦SEO
汇亚创意设计
酷勤网
樂思蜀SEO博客
(rss)
李天平
(rss)
网络安全
兄弟blog
积分与排名
积分 - 631746
排名 - 78
最新评论
阅读排行榜
评论排行榜