明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

將一個(gè)圖片以二進(jìn)制值的形式存入Xml文件中

[摘要]try int readByte = 0; // int bytesToRead = 100; //數(shù)據(jù)緩沖區(qū)大小 string fileName = "../../WriteXml.xml"; //要打開(kāi)的文件 //...
try
   {
    int readByte = 0;        //
    int bytesToRead = 100;       //數(shù)據(jù)緩沖區(qū)大小
    string fileName = "../../WriteXml.xml";   //要打開(kāi)的文件
    //   this.textBox1.Text = string.Empty;           

    // 打開(kāi)圖片文件,利用該圖片構(gòu)造一個(gè)文件流
    FileStream fs = new FileStream("../../001.jpg",FileMode.Open);
    // 使用文件流構(gòu)造一個(gè)二進(jìn)制讀取器將基元數(shù)據(jù)讀作二進(jìn)制值
    BinaryReader br = new BinaryReader(fs);


    XmlTextWriter xmlTxtWt = new XmlTextWriter(fileName,Encoding.UTF8);
    //輸出設(shè)置 代碼縮進(jìn)
    xmlTxtWt.Formatting = Formatting.Indented;
    //   xmlTxtWt.Indentation = 4;

    //書寫聲明
    xmlTxtWt.WriteStartDocument();
    xmlTxtWt.WriteStartElement("picture","ContactDetails","http://www.deltabis.com/Contact");//定義命名空間
    xmlTxtWt.WriteStartElement("image");            //定義節(jié)點(diǎn)
    xmlTxtWt.WriteAttributeString("imageName","002.jpg");        //添加圖片屬性

    byte[] base64buffer = new byte[bytesToRead];          //開(kāi)辟緩沖區(qū)
    do
    {
     readByte = br.Read(base64buffer,0,bytesToRead);      //將數(shù)據(jù)讀入字節(jié)數(shù)組
     xmlTxtWt.WriteBase64(base64buffer,0,readByte);       //將數(shù)組中二進(jìn)制值編碼為Base64并寫出到XML文件

    }while(bytesToRead <= readByte);


    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndElement();
    xmlTxtWt.WriteEndDocument();


//    xmlTxtWt.Flush();
    xmlTxtWt.Close();

    MessageBox.Show("讀寫結(jié)束!");

    //   this.textBox1.Text = ReadXml(fileName);
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.ToString());
   }