电子公文数据交换的解决方法
XML在数据交换中的作用越来越大。在公文无纸化传输中,传输的不仅有文本,还有签名、word文件、其他附件等等,如何将这些二进制文件嵌入XML中进行传输,这里以嵌入一个签名图片为例,做简单介绍。
1.将二进制文件以Base64格式写入XML
public void InsertFileToXml()
{
FileStream fs = File.OpenRead("d:\\sign.gif");
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
XmlTextWriter writer = new XmlTextWriter("d:\\user.xml",System.Text.Encoding.Default);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("User");
writer.WriteElementString("First_Name", "John");
writer.WriteElementString("Last_Name", "Wang");
writer.WriteStartElement("Signature");
writer.WriteAttributeString("filename","sign.gif");
writer.WriteBase64(buffer,0,buffer.Length);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
{
FileStream fs = File.OpenRead("d:\\sign.gif");
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
XmlTextWriter writer = new XmlTextWriter("d:\\user.xml",System.Text.Encoding.Default);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("User");
writer.WriteElementString("First_Name", "John");
writer.WriteElementString("Last_Name", "Wang");
writer.WriteStartElement("Signature");
writer.WriteAttributeString("filename","sign.gif");
writer.WriteBase64(buffer,0,buffer.Length);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
生成后的XML(user.xml)内容如下:
<?xml version="1.0" encoding="gb2312"?>
<User>
<First_Name>John</First_Name>
<Last_Name>Wang</Last_Name>
<Signature filename="sign.gif">R0lGODlhZAAZAIAAAJ0IDf///yH5BAAAAAAALAAAAABkABkAAAKXjI+py+0Po5y02ouz3rz7D4biSJbmiabqAbQr2sbANL+Ja9WNzDv6qvvlJMKdLVD74WjESJH1HLKQhuSSEoU2FzHGEqdEBmfW5CV7FaepXe3za4YKs1Tp7a2V16t8hZV9h5WhhJZXFSfWdzfGQffQ49OGEDSleOIIQ+mngnk5eWQIKsrVOcpJZpqqWKpKotkq+gp7JKtQAAA7</Signature>
</User>
<User>
<First_Name>John</First_Name>
<Last_Name>Wang</Last_Name>
<Signature filename="sign.gif">R0lGODlhZAAZAIAAAJ0IDf///yH5BAAAAAAALAAAAABkABkAAAKXjI+py+0Po5y02ouz3rz7D4biSJbmiabqAbQr2sbANL+Ja9WNzDv6qvvlJMKdLVD74WjESJH1HLKQhuSSEoU2FzHGEqdEBmfW5CV7FaepXe3za4YKs1Tp7a2V16t8hZV9h5WhhJZXFSfWdzfGQffQ49OGEDSleOIIQ+mngnk5eWQIKsrVOcpJZpqqWKpKotkq+gp7JKtQAAA7</Signature>
</User>
2.从XML中读取文件
XmlTextReader reader = new XmlTextReader(@"d:\user.xml");
while (reader.Read())
{
if (reader.Name == "Signature" && reader.NodeType == XmlNodeType.Element)
{
byte[] buffer = new byte[1024];
int size;
FileStream file = File.Create("d:\\" + reader.GetAttribute("filename"));
while (true)
{
size = reader.ReadBase64(buffer,0,1024);
if (size>0)
{
file.Write(buffer,0,size);
}
else
{
break;
}
}
file.Close();
}
}
while (reader.Read())
{
if (reader.Name == "Signature" && reader.NodeType == XmlNodeType.Element)
{
byte[] buffer = new byte[1024];
int size;
FileStream file = File.Create("d:\\" + reader.GetAttribute("filename"));
while (true)
{
size = reader.ReadBase64(buffer,0,1024);
if (size>0)
{
file.Write(buffer,0,size);
}
else
{
break;
}
}
file.Close();
}
}
posted on 2005-05-16 17:53 ~~John 的网络涂鸦纪实~~ 阅读(407) 评论(0) 编辑 收藏 举报