A Save a picture to an XML file
Copy Code code as follows:
<summary>
Save a picture to an XML file
</summary>
private void Uploadimagetoxml ()
{
Get the filename the user wants to upload
string strfilepathname = LoFile.PostedFile.FileName;
String strFileName = Path.getfilename (strfilepathname);
int filelength = LoFile.PostedFile.ContentLength;
if (filelength<=0)
Return
Try
{
Image file temporary storage byte array
byte[] Filebytearray = new Byte[filelength];
Set up a data stream pair like
Stream streamobject = LoFile.PostedFile.InputStream;
Read image file data, Filebytearray for data storage, 0 for data pointer position, filelnegth for data length
Streamobject.read (filebytearray,0,filelength);
The file to open
String fileName = Server.MapPath (". \\WriteXml.xml");
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (FileName);
Find <dbGuest>
XmlNode Root=xmldoc.selectsinglenode ("Dbimage");
XmlNodeList Xnl=xmldoc.selectsinglenode ("Dbimage"). ChildNodes;
int nindex = xnl. Count;
Add new nodes below
XmlElement xe1=xmldoc.createelement ("Image");//Create a <User> node
XmlElement xesub1=xmldoc.createelement ("imageID");
Set text node
Xesub1. Innertext=nindex.tostring ();
Add to <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 <dbGuest> node
Root. AppendChild (XE1);
Xmldoc.save (FileName);
Response.Redirect ("showallimg.aspx");
}
catch (Exception ex)
{
Throw ex;
}
}
Two Reading picture data from XML
Copy Code code as follows:
<summary>
Reading pictures from XML
</summary>
<param name= "imageID" > Pictures id</param>
private void Readimagefromxml (string imageID)
{
ID is picture ID
int imgid = Convert.ToInt32 (imageID);
The file to open
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 Output File type
Response.ContentType = Strtype;
Output image File binary system
Response.OutputStream.Write (Convert.frombase64string (strdata), 0, nsize);
Response.End ();
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 ();
}
}