打印

[XML教程] 将一个图片以二进制值的形式存入xml文件中[xml]

将一个图片以二进制值的形式存入xml文件中[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());

TOP

返回顶部
AYBlue

Processed in 0.058519 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 05:25 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP