用C#把文件转换为XML,第1张

用C#把文件转换为XML,第2张

使用系统;
使用系统。绘图;
使用系统。收藏;
使用系统。组件模型;
使用系统。Windows . Forms
使用系统。木卫一;
使用系统。XML
Namespace My Windows
{
/
//这个例子演示了如何将Office文件编码成xml文件,以及如何将生成的xml文件转换成Office文件
//文件转换成XML格式,然后就可以使用Web服务了,。net Remoting、WinSock等。都是传输的(后两者无需转换即可传输)
//xml解决了多层架构中的数据传输问题。比如客户端可以使用Web服务获取服务器的office文件,然后修改并发回到服务器
//只要把文件转换成xml格式就可以了,可以使用的方案有很多,而且XML是平台无关的。您可以使用发布web服务。net,然后在客户端用
//Java写一个applit小程序来处理发送过来的文件。当然,我举的例子几乎没有什么意义,但是给了我们很多启发。
//另外,如果你的解决方案是基于多个平台的,那么它们之间的交互应该基于文档而不是远程应用程序接口调用(RPC)。
//例如,MSMQ治下。net和J2EE的JMQ。
//
//例中设计的类很多,各个地方我都没有做过多的注释,不太懂。这是我的第一个windows程序,有问题
///欢迎大家指导
//
公开课form 1:system . windows . forms . form
{

///
//声明四个按钮,一个OpenFileDialog、一个SaveFileDialog和两个XML documents
//
private system . windows . forms . Button Button 1;
私有系统。Windows . Forms.Button button2
私有系统。windows . forms . openfile dialog openfile dialog 1;
私有系统。windows . forms . save file dialog save file dialog 1;
私有系统。Windows . Forms.Button button3
私有系统。Windows . Forms.Button button4
私有系统。Xml . XmlDocument mXmlDoc
私有系统。Xml.XmlDocument文档;
私有系统。ComponentModel.Container组件= null

public Form 1()
{
/
/Windows窗体设计器支持必要的
/
initialize component();

//
// TODO:在InitializeComponent调用之后添加任何构造函数代码
/
}

///
//清理所有正在使用的资源。
///
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components!= null)
{
组件。dispose();
}
}
base。Dispose(处置);
}

# Region Windows窗体设计器生成的代码
//
//设计器支持所需的方法-不要使用代码编辑器修改
//此方法的内容。
///
private void initialize component()
{
this . button 1 =新系统。windows . forms . button();
this.button2 =新系统。windows . forms . button();
this.openFileDialog1 =新系统。windows . forms . openfile dialog();
this.saveFileDialog1 =新系统。windows . forms . savefiledialog();
this.button3 =新系统。windows . forms . button();
this.button4 =新系统。windows . forms . button();
这个。suspend layout();
//
//button 1
//
this . button 1 . location =新系统。绘图点(96,32);
this . button 1 . name = " button 1 ";
this . button 1 . tabindex = 0;
this.button1.Text = "生成XML ";
this.button1.Click +=新系统。EventHandler(this . button 1 _ Click);
//
//button 2
//
this . button 2 . location =新系统。绘图点(96,80);
this . button 2 . name = " button 2 ";
this . button 2 . tabindex = 1;
this.button2.Text = "生成单据";
this.button2.Click +=新系统。EventHandler(this . button 2 _ Click);
//
//button 3
//
this . button 3 . location =新系统。绘图点(8,32);
this . button 3 . name = " button 3 ";
this . button 3 . tabindex = 2;
this . button 3 . text = " load doc ";
this.button3.Click +=新系统。EventHandler(this . button 3 _ Click);
//
//button 4
//
this . button 4 . location =新系统。绘图.点(8,80);
this . button 4 . name = " button 4 ";
this . button 4 . tabindex = 3;
this . button 4 . text = " load XML ";
this.button4.Click +=新系统。EventHandler(this . button 4 _ Click);
//
//form 1
//
this。AutoScaleBaseSize =新系统。图纸。尺寸(6,14);
这个。ClientSize =新系统。图纸。尺寸(184,141);
这个。controls . Add(this . button 4);
这个。controls . Add(this . button 3);
这个。controls . Add(this . button 2);
这个。controls . Add(this . button 1);
这个。Name = " Form1
这个。Text = " Form1
这个。ResumeLayout(false);
/
//手动注册加载和关闭事件
/
this . Load+= new system . eventhandler(this . form 1 _ Load);
这个。封闭+=新系统。EventHandler(this。Form1 _已关闭);

}
#endregion

///
//从此门户启动表单
//
static void main()
{
application . run(new form 1());
}
//
//将加载的Office文件转换为xml文件
//
/
Private void button 1 _ click(对象发送系统。eventargs e)
{
savefiledialog 1 . filter = " XML file | *。XML ";//设置打开对话框的文件过滤条件
saveFileDialog1。Title = "另存为xml文件";//设置打开对话框的标题
savefiledialog 1 . filename = " ";
saveFileDialog1。ShowDialog();//打开对话框

if(saveFileDialog1。文件名!= " ")//检查用户是否输入了保存文件名
{
mxmldoc . save(save file dialog 1 . filename);//用私有对象mXmlDoc保存文件,该私有对象之前已经声明了
MessageBox。Show("保存成功");
}
}

///
//将加载的xml文件转换为Office文件
//
//
Private void button 2 _ click(对象发送方,系统。eventargs e)
{
/从私有对象dox中选择me节点。更多信息请参考msdn
xmlNode = doc . documentelement . selectsinglenode(" me ")此处提供了XML对象操作的一些详细说明;
XML element ele =(XML element)节点;//获取一个xml元素
string pic = ele . Get attribute(" AA ");//获取ele元素的aa属性,并在临时字符串变量pic中报告

byte[]bytes =转换。from base 64 string(pic);//声明一个byte[]存储Base64解码转换的数据流

/从保存对话框中获取文件保存地址
savefiledialog 1 . filter = " Office documents(*。医生,*。xls,*。ppt) | *。doc*.xls*.PPT”;
saveFileDialog1。Title = "另存为office文件";
saveFileDialog1。文件名= " ";
saveFileDialog1。ShowDialog();

if(saveFileDialog1。文件名!= " ")
{
/创建文件流并保存
filestream outfile = new system . io . filestream(save file dialog 1 . filename,system . io . filemode . new);
outfile。写入(字节,0,(int)字节。长度);
MessageBox。Show("保存成功");
}

}

///
//加载windows时的一些初始化行为
//
//
public void form 1 _ load(对象发送方,系统。eventargs e)

}
//
//卸载表单时释放所有临时变量
//
/
publicvidform 1 _ closed(Object Sends
doc = null;
}
//
//加载office文件并将序列编码为XmlDocument变量
//
/
privatevoiddutton 3 _ click System。EventArgs e)
{
string strFileName;
openFileDialog1。Filter = "Office文档(*。医生,*。xls,*。ppt)|*。doc*.xls*.PPT”;
openFileDialog1。filter index = 1;
openFileDialog1。文件名= " ";
openFileDialog1。ShowDialog();
strFileName = openfile dialog 1。文件名;
if(strFileName。长度!= 0)
{
System。IO . FileStream inFile = new FileStream(strFileName,System。IO.FileMode.Open,System。IO . file access . read);
byte[]binary data = new byte[inFile。长度];
inFile。Read(binaryData,0,(int)inFile。长度);
string mStr=Convert。to base 64 string(binary data);
string hh = mStr;
mXmlDoc =新系统。XML . XML document();

mStr=string。格式(",mStr);
mXmlDoc。load XML(mStr);
MessageBox。Show("成功加载");
}

}
///
//将xml文件加载到私有对象DOX
//
//
privatevidbutton 4 _ click(object sender,system . eventargs
openfile dialog 1 . filter = " XML file | *;XML ";
openFileDialog1。filter index = 1;
openFileDialog1。文件名= " ";
openFileDialog1。ShowDialog();
strFileName = openfile dialog 1。文件名;
//如果用户不取消,则打开文档。
if(strFileName。长度!= 0)
{
doc = new XML document();
doc。load(strFileName);
MessageBox。Show("成功加载");
}

}

}
}

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » 用C#把文件转换为XML

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情