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.jb51.net");//定義命名空間
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());
}