[转]电子公文数据交换的解决方法

 XML在数据交换中的作用越来越大。在公文无纸化传输中,传输的不仅有文本,还有签名、word文件、其他附件等等,如何将这些二进制文件嵌入XML中进行传输,这里以嵌入一个签名图片为例,做简单介绍。

1.将二进制文件以Base64格式写入XML

None.gif          public void InsertFileToXml()
ExpandedBlockStart.gif ContractedBlock.gif         dot.gif {
InBlock.gif            FileStream fs 
=  File.OpenRead( "d:\\sign.gif");
InBlock.gif             byte [] buffer  =   new byte[fs.Length];
InBlock.gif            fs.Read(buffer, 0, buffer.Length);
InBlock.gif
InBlock.gif            XmlTextWriter writer  =   new  XmlTextWriter( "d:\\user.xml",System.Text.Encoding.Default);
InBlock.gif            
InBlock.gif            writer.Formatting = Formatting.Indented;
InBlock.gif            writer.WriteStartDocument();
InBlock.gif            writer.WriteStartElement( "User");
InBlock.gif            writer.WriteElementString( " First_Name " "John");
InBlock.gif            writer.WriteElementString( " Last_Name " "Wang");
InBlock.gif
InBlock.gif            writer.WriteStartElement( "Signature");
InBlock.gif            writer.WriteAttributeString( " filename " , "sign.gif");
InBlock.gif            writer.WriteBase64(buffer,0,buffer.Length);
InBlock.gif
InBlock.gif            writer.WriteEndElement();
InBlock.gif            writer.WriteEndElement();
InBlock.gif            writer.WriteEndDocument();
InBlock.gif
InBlock.gif            writer.Flush();
InBlock.gif            writer.Close();
ExpandedBlockEnd.gif        }
None.gif


生成后的XML(user.xml)内容如下:

 

 

None.gif <?xml version="1.0" encoding="gb2312"?>
None.gif<User>
None.gif  < First_Name > John </First_Name>
None.gif  < Last_Name > Wang </Last_Name>
None.gif  < Signature  filename ="sign.gif" > R0lGODlhZAAZAIAAAJ0IDf///yH5BAAAAAAALAAAAABkABkAAAKXjI+py+0Po5y02ouz3rz7D4biSJbmiabqAbQr2sbANL+Ja9WNzDv6qvvlJMKdLVD74WjESJH1HLKQhuSSEoU2FzHGEqdEBmfW5CV7FaepXe3za4YKs1Tp7a2V16t8hZV9h5WhhJZXFSfWdzfGQffQ49OGEDSleOIIQ+mngnk5eWQIKsrVOcpJZpqqWKpKotkq+gp7JKtQAAA7 </Signature>
None.gif</ User >


2.从XML中读取文件

None.gif             XmlTextReader reader  =   new  XmlTextReader( @" d:\user.xml");
None.gif            while (reader.Read())
ExpandedBlockStart.gif ContractedBlock.gif             dot.gif {
InBlock.gif                
if  (reader.Name  ==   " Signature "   &&  reader.NodeType == XmlNodeType.Element)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                 dot.gif {
InBlock.gif
InBlock.gif                    
byte [] buffer  =   new   byte [1024];
InBlock.gif                    int size;
InBlock.gif
InBlock.gif                    FileStream file  =  File.Create( " d:\\ "   +  reader.GetAttribute( " filename"));
InBlock.gif
InBlock.gif                     while  (true)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                     dot.gif {
InBlock.gif                        size 
=  reader.ReadBase64(buffer, 0 ,1024);
InBlock.gif                         if  (size >0)
ExpandedSubBlockStart.gif ContractedSubBlock.gif                         dot.gif {
InBlock.gif                            file.Write(buffer,
0,size);
ExpandedSubBlockEnd.gif                        }
InBlock.gif                        else
ExpandedSubBlockStart.gif ContractedSubBlock.gif                         dot.gif {
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    file.Close();
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

转载于:https://www.cnblogs.com/monica/archive/2010/07/09/1774585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值