Copy codeThe Code is as follows:
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.jb51.net"); // 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 ());
}