Dom|xml
1, need the name space
Using System.Text;
Using System.IO;
Using System.Xml;
2, there are 001.jpg pictures in the folder
3. Generate a Docsave.xml file
//******************
Try
{
XmlDocument myxmldoc = new XmlDocument ();
Myxmldoc.loadxml ("<picture><name>picture</name></picture>");
XmlElement elem = myxmldoc.createelement ("image");
Open a picture file and use the picture to construct a file stream
FileStream fs = new FileStream (".. /.. /001.jpg ", FileMode.Open);
Constructs 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 end!") ");
catch (Exception ex)
{
MessageBox.Show (ex. ToString ());
}
//*************
Post-generated XML document
//******
<picture>
<name>picture</name>
<image>......</image>
</picture>
//***********
Test the previous program
//***********
Note: Read the image node in Docsave.xml and save it as a picture 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")
{
Todo
{
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 end!") ");
}
catch (Exception ex)
{
MessageBox.Show (ex. ToString ());
}