//********************************* { XmlDocument myXmlDoc = new XmlDocument(); myXmlDoc.LoadXml("<picture><name>picture</name></picture>"); XmlElement elem = myXmlDoc.CreateElement("image"); // 打开图片文件,利用该图片构造一个文件流 FileStream fs = new FileStream("../../001.jpg",FileMode.Open); // 使用文件流构造一个二进制读取器将基元数据读作二进制值 BinaryReader br = new BinaryReader(fs); byte[] imageBuffer = new byte[br.BaseStream.Length]; br.Read(imageBuffer,0,Convert.ToInt32(br.BaseStream.Length)); string textString = System.Convert.ToBase64String(imageBuffer); fs.Close(); br.Close(); XmlText text = myXmlDoc.CreateTextNode(textString); myXmlDoc.DocumentElement.AppendChild(elem); myXmlDoc.DocumentElement.LastChild.AppendChild(text); myXmlDoc.Save("../../docSave.xml"); MessageBox.Show("读写结束!"); catch(Exception ex) { MessageBox.Show(ex.ToString()); } //************************************************ //生成后的Xml文档 //****** <picture> <name>picture</name> <image>......</image> </picture> //************************************************* //测试上一程序 //*********** |