Select the Blog from xinyuxin912 to save an image to the Xml file in binary format. try {intreadByte = 0; // intbytesToRead = 100; // data buffer size stringfileName = .. /.. /WriteXml. xml; // the file to be opened // this
Select Blog from xinyuxin912
Store an image in the Xml file as a binary value.
Try
{
Int readByte = 0 ;//
Int bytesToRead = 100; // data buffer size
String fileName = "../WriteXml. xml"; // file to be opened
// This. textBox1.Text = string. Empty;
// Open an image file and use the image to construct a File Stream
FileStream fs = new FileStream (".../../001.jpg", FileMode. Open );
// Construct a binary reader using a file stream to read the base metadata as a binary value
BinaryReader br = new BinaryReader (fs );
XmlTextWriter xmlTxtWt = new XmlTextWriter (fileName, Encoding. UTF8 );
// Output code indent settings
XmlTxtWt. Formatting = Formatting. Indented;
// XmlTxtWt. Indentation = 4;
// Write the statement
XmlTxtWt. WriteStartDocument ();
XmlTxtWt. WriteStartElement ("picture", "ContactDetails", "http://www.deltabis.com/Contact"); // defines the namespace
XmlTxtWt. WriteStartElement ("image"); // defines a node.
XmlTxtWt. WriteAttributeString ("imageName", "002.jpg"); // Add image attributes
Byte [] base64buffer = new byte [bytesToRead]; // open a buffer
Do
{
ReadByte = br. Read (base64buffer, 0, bytesToRead); // Read data into byte arrays
XmlTxtWt. WriteBase64 (base64buffer, 0, readByte); // Encode the binary value in the array to Base64 and write it to the XML file
} While (bytesToRead <= readByte );
XmlTxtWt. WriteEndElement ();
XmlTxtWt. WriteEndElement ();
XmlTxtWt. WriteEndDocument ();
// XmlTxtWt. Flush ();
XmlTxtWt. Close ();
MessageBox. Show ("read and write ends! ");
// This. textBox1.Text = ReadXml (fileName );
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}