将一个图片以二进制值的形式存入Xml文件中
来源:互联网 时间:2008-09-04 11:24:00
选择自 xinyuxin912 的 Blog
将一个图片以二进制值的形式存入Xml文件中
try
{
int readByte = 0; //
int bytesToRead = 100; //数据缓冲区大小
string fileName = "../../WriteXml.xml"; //要打开的文件
// this.textBox1.Text = string.Empty;
// 打开图片文件,利用该图片构造一个文件流
FileStream fs = new FileStream("../../001.jpg",FileMode.Open);
// 使用文件流构造一个二进制读取器将基元数据读作二进制值
BinaryReader br = new BinaryReader(fs);
XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8);
//输出设置 代码缩进
xmlTxtWt.Formatting = Formatting.Indented;
// xmlTxtWt.Indentation = 4;
//书写声明
xmlTxtWt.WriteStartDocument();
xmlTxtWt.WriteStartElement("picture","ContactDetails","http://www.deltabis.com/Contact");//定义命名空间
xmlTxtWt.WriteStartElement("image"); //定义节点
xmlTxtWt.WriteAttributeString("imageName","002.jpg"); //添加图片属性
byte[] base64buffer = new byte[bytesToRead]; //开辟缓冲区
do
{
readByte = br.Read(base64buffer,0,bytesToRead); //将数据读入字节数组
xmlTxtWt.WriteBase64(base64buffer,0,readByte); //将数组中二进制值编码为Base64并写出到XML文件
}while(bytesToRead <= readByte);
xmlTxtWt.WriteEndElement();
xmlTxtWt.WriteEndElement();
xmlTxtWt.WriteEndDocument();
// xmlTxtWt.Flush();
xmlTxtWt.Close();
MessageBox.Show("读写结束!");
// this.textBox1.Text = ReadXml(fileName);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用Dreamweaver制作活动菜单条
![](https://img.aspxhome.com/file/UploadPic/20072/200723113334999s.jpg)
下拉框二级联动的JavaScript代码
ASP+ajax注册即时提示程序代码
如何修复MySQL数据库表
IE6 bug: 消失的绝对定位元素
Typecho插件实现添加文章目录的方法详解
详解MySQL数据库安全配置
利用SQL语句对不同数据库进行高效果分页
如何不通过DSN访问SQL Server?
表单制作方式大比拼
![](https://img.aspxhome.com/file/uploadpic/20071/2007131102013522.gif)
如何控制弹出一个NTLM验证窗口?
![](https://img.aspxhome.com/file/UploadPic/200912/16/ntlm-36s.gif)
css样式表滤镜全接触
图片滤镜效果[IE Only]
Request.ServerVariables各参数说明集合
网页模式化窗口
![](/images/sg_trans.gif)
centos 安装mysql中遇到问题的解决办法
一个将半角"转换为中文"的asp函数
评论是倒序好还是顺序好?
![](https://img.aspxhome.com/file/UploadPic/200711/9/2007119104745707s.png)