How to save an image to an XML file in asp.net

Source: Internet
Author: User

1. Save the image to an XML file
Copy codeThe 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 codeThe 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 ();
}
}

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.