How to save a picture to an XML file under asp.net practical tips

Source: Internet
Author: User
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 ();
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.