Upload image 3. generate a docSave. xml file //*********************************
Read the image into the Dom and save it as an xml file.
1. namespace required
Using System. Text;
Using System. IO;
Using System. Xml;
2's folder contains jpg images
3. generate a docSave. xml file
//*********************************
{
XmlDocument myXmlDoc = new XmlDocument ();
MyXmlDoc. LoadXml (" Picture ");
XmlElement elem = myXmlDoc. CreateElement ("image ");
// 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 );
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 ("read and write ends! ");
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
//************************************** **********
// Generated Xml document
//******
Picture
......
//************************************** ***********
// Test the previous program
//***********
Description: read the image node in docsave.xmland save it as image format 002.jpg.
Try
{
Int readByte = 0;
Int bytesToRead = 1044;
XmlTextReader xmlTxtRd = new XmlTextReader (".../../docSave. xml ");
FileStream fs = new FileStream (".../002.jpg", FileMode. Create );
BinaryWriter bw = new BinaryWriter (fs );
Byte [] base64buffer = new byte [bytesToRead];
While (xmlTxtRd. Read ())
{
If (xmlTxtRd. NodeType = XmlNodeType. Element & xmlTxtRd. Name = "image ")
{
Do
{
ReadByte = xmlTxtRd. ReadBase64 (base64buffer, 0, bytesToRead );
Bw. Write (base64buffer, 0, readByte );
}
While (bytesToRead <= readByte );
}
}
Bw. Flush ();
Bw. Close ();
Fs. Close ();
XmlTxtRd. Close ();
MessageBox. Show ("read and write ends! ");
}
Catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}