1. Save the image to an XML file
CopyCode The Code is as follows: // <summary>
/// Save the image to an XML file
/// </Summary>
Private void uploadimagetoxml ()
{
/// Get the file name to be uploaded
String strfilepathname = lofile. postedfile. filename;
String strfilename = path. getfilename (strfilepathname );
Int filelength = lofile. postedfile. contentlength;
If (filelength <= 0)
Return;
Try
{
/// Temporarily store byte Arrays for image files
Byte [] filebytearray = new byte [filelength];
/// Create a data stream object
Stream streamobject = lofile. postedfile. inputstream;
/// Read image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
Streamobject. Read (filebytearray, 0, filelength );
/// File to be opened
String filename = server. mappath (". \ writexml. xml ");
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename );
/// Search for <dbguest>
Xmlnode root = xmldoc. selectsinglenode ("dbimage ");
Xmlnodelist xnl = xmldoc. selectsinglenode ("dbimage"). childnodes;
Int nindex = xnl. count;
/// Add the following Node
Xmlelement xe1 = xmldoc. createelement ("image"); // create a <user> node
Xmlelement xesub1 = xmldoc. createelement ("imageid ");
/// Set the text node
Xesub1.innertext = nindex. tostring ();
/// Add to the <user> node
Xe1.appendchild (xesub1 );
Xmlelement xesub2 = xmldoc. createelement ("imagecontenttype ");
Xesub2.innertext = lofile. postedfile. contenttype;
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("imagesize ");
Xesub3.innertext = filelength. tostring ();
Xe1.appendchild (xesub3 );
Xmlelement xesub4 = xmldoc. createelement ("imagedescription ");
Xesub4.innertext = tbdescription. text;
Xe1.appendchild (xesub4 );
Xmlelement xesub5 = xmldoc. createelement ("imagedata ");
Xesub5.innertext = convert. tobase64string (filebytearray );
Xe1.appendchild (xesub5 );
/// Add to the <dbguest> node
Root. appendchild (xe1 );
Xmldoc. Save (filename );
Response. Redirect ("showallimg. aspx ");
}
Catch (exception ex)
{
Throw ex;
}
}
Ii. Reading image data from XML
Copy code The Code is as follows: // <summary>
/// Read the image from XML
/// </Summary>
/// <Param name = "imageid"> image id </param>
Private void readimagefromxml (string imageid)
{
/// ID is the image ID
Int imgid = convert. toint32 (imageid );
/// File to be opened
String filename = server. mappath (". \ writexml. xml ");
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (filename );
Xmlnodelist node = xmldoc. selectsinglenode ("// image [imageid = '" + imgid. tostring () + "']"). childnodes;
If (node! = NULL)
{
String strtype = node. Item (1). innertext;
String strdata = node. Item (4). innertext;
Int nsize = int. parse (node. Item (2). innertext );
/// Set the output file type
Response. contenttype = strtype;
/// Binary number of output image files
Response. outputstream. Write (convert. frombase64string (strdata), 0, nsize );
Response. End ();
// It can also be saved as an image
// Filestream FS = new filestream (@ "C: \ AA. BMP", filemode. openorcreate, fileaccess. Write );
// Fs. Write (convert. frombase64string (strdata), 0, nsize );
// Fs. Close ();
}
}